I have the following Javascript regexp:
var regex = '/^' + name + '/';
var s ='';
s = this.innerHTML.toString().toLowerCase().match(regex);
if (s != null){
//do stuff
}
This regex does not work as expected, s
never gets set (s = null
always)
Any ideas?
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp , and with the match() , matchAll() , replace() , replaceAll() , search() , and split() methods of String .
* + ( ) literally, we need to prepend them with a backslash \ (“escape them”). We also need to escape / if we're inside /.../ (but not inside new RegExp ). When passing a string to new RegExp , we need to double backslashes \\ , cause string quotes consume one of them.
matchAll() The matchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups.
match() is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. Parameters: Here the parameter is “regExp” (i.e. regular expression) which will compare with the given string.
var regex = new RegExp("^" + name);
Maybe this fixes the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With