Sponsored Links

December 5, 2017

Regular Expressions in JavaScript with Examples

Example 1 : To return all the matched strings
var a = "This is regular expression example.";
var regex = /regular/g; // to find string regular
var matches = a.match(regex);


Example 2:  To return all matched numbers
var a = "This is 1 apple, this is 2 apple";
var regex = /\d+/g;
var matches = a.match(regex);

Example 3:  To return number of spaces in the sentence
var a = "How many spaces are there in this sentence?";
var regex = /\s+/g;
var matches = a.match(regex).length;

No comments:

Post a Comment