How do I give expression inside match function in javascript?
Eg.
var val = $('#somElement').val();
var startWith = '#'
var match = val.match('/(\s)?' + startWith + '(.*?)(\s+|$)/'g);
The match function takes in a parameter without string and hence the above line is wrong. How do I change it so that I can give in the parameter to val.match dynamically based on my variable’s value?
You need to explicitly construct a RegExp object:
Note that backslashes need to be doubled, to account for the fact that the string constant syntax also uses backslashes for special-character quoting, and that you don’t need the leading and trailing forward-slash.