0

i have this function in php :

$html_price = "text text 12 eur or $ 22,01 text text";
preg_match_all('/(?<=|^)(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?)(?:\ |)(?:\$|usd|eur)+(?=|$)|(?:\$|usd|eur)(?:| )(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?)/', strtolower($html_price), $price_array1);
print_r($price_array1);

Now i want use in javascript the same regex but i have an error: SyntaxError: invalid quantifier

my javascript :

maxime_string = "((?<=|^)(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?)(?:\ |)(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€)+(?=|$)|(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€)(?:| )(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?))";
maxime_regex = new RegExp(maxime_string);
var text = "text text 12 eur or $ 22,01 text text";          
var result = text.match(maxime_regex);
var max = result.length;
alert(result.length);
for (i = 0; i < max; i++) 
{ 
document.write(result[i] + "<br/>");
} 

Can you help me ?

2
  • 2
    there's no lookbehind ?<= in javascript regular expressions Commented May 27, 2013 at 17:54
  • 1
    @bokonic: You should make that an answer. That and the fact that the OP didn't escape the backslashes correctly. Commented May 27, 2013 at 17:57

1 Answer 1

6

JavaScript does not support lookbehind - that's probably where you got your error from. Some things:

  • (?<=|^) makes no sense. It's a lookbehind demanding either nothing or string begin to come before this - it's always true. And if you want to match the string start, use a single ^.
  • (?:.[0-9]*)? - I guess you wanted to escape the dot so it matches literally
  • (?:\ |): You do not need to escape blanks. And instead of an alternative "nothing", you should just make the blank optional: " ?".
  • (?=|$) makes as much sense as the first group.
  • € is a broken unicode character?

Also, you should consider using a regex literal instead of the RegExp constructor with a string literal - it eats one level of backslash escaping. And it seems you're missing the global flag, so that you get back all matches. Try this:

var maxime_regex = /((?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:\.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?) ?(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€)+|(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€) ?(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:\.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?))/g

This post does not include any effort to understand, optimize or shorten that regex monster

Sign up to request clarification or add additional context in comments.

6 Comments

you wouldn't need to pass a literal to the RegExp constructor; it's already a RegExp object
@bokonic: That's what I meant of course. Edited to clarify :-)
@user2411447: And that problem is?
if i want to find 22,3 $ the script return 3 $ , i want "22,3 $", in php with preg_match it's working it's a problem with the coma i think
"… 22,3 $ …".match(maxime_regex) does return Array ["22,3 $"] for me? Please provide explicit input and expected output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.