I have the following php function which i wrote a while ago. Now however, I’m moving some parsing/matching to the client side.
function artist_name_to_regex($art_name){
$regex_part = preg_quote($art_name);
$regex_part = preg_replace('('|\')', '.+', $regex_part);
$regex_part = preg_replace('/ /i', '\s?', $regex_part);
$regex_part = preg_replace('/(and|&|&|\+)/i', '(and|&|&|\+)', $regex_part);
return $regex_part;
}
I’d like to call it like this from js:
var regex = artist_name_to_regex('David & the Smokey Sea Horses!');
if(some_str.match(/regex/ig)){
alert('match found!');
}
I need to modify the top function so that it:
a) is written in javascript
b) returns a regex that will work with javascipt
Why do you need to construct a regular expression like that? It looks as if you need to know whether a string occurs within another one…
Which can much more easily be tested like this:
Otherwise, it’s fairly easy to create a regular expression from a string: