16

i am working in regex my regex is /\[([^]\s]+).([^]]+)\]/g this works great in PHP for [http://sdgdssd.com fghdfhdhhd] but when i use this regex for javascript it do not match with this input string

my input is [http://sdgdssd.com fghdfhdhhd]

3
  • 1
    You sure your statement is correct? I'd imagine /\[([^]\s]+).([^]]+)\]/g might work in JS but not PHP; PHP has different modifiers than JS. g is not a PHP modifier. Commented Dec 7, 2015 at 13:37
  • this works on PHP you can check it by execute on regex101.com Commented Dec 7, 2015 at 13:57
  • 1
    regex101 isn't a PHP interpretor. That is just a regex tester. eval.in/481587 See php.net/manual/en/reference.pcre.pattern.modifiers.php The regex is valid but it won't work in PHP with that modifier. The PHP functions are global already or there is a function for it to be global (preg_match_all vs. preg_match).. Commented Dec 7, 2015 at 14:00

2 Answers 2

13

In JavaScript regex, you must always escape the ] inside a character class:

\[([^\]\s]+).([^\]]+)\]

See the regex demo

JS parsed [^] as *any character including a newline in your regex, and the final character class ] symbol as a literal ].

In this regard, JS regex engine deviates from the POSIX standard where smart placement is used to match [ and ] symbols with bracketed expressions like [^][].

The ] character is treated as a literal character if it is the first character after ^: [^]abc].

In JS and Ruby, that is not working like that:

You can include an unescaped closing bracket by placing it right after the opening bracket, or right after the negating caret. []x] matches a closing bracket or an x. [^]x] matches any character that is not a closing bracket or an x. This does not work in JavaScript, which treats [] as an empty character class that always fails to match, and [^] as a negated empty character class that matches any single character. Ruby treats empty character classes as an error. So both JavaScript and Ruby require closing brackets to be escaped with a backslash to include them as literals in a character class.

Related:

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

3 Comments

That's strange the the ] doesn't need to be escaped in PCRE. I've always escaped just out of instinct because I thought it would choke on that if not.
@chris85: I just edited the answer since I double checked and found [ does not need to be escaped inside a character class in JS.
Added some more explanations related to this issue.
2

I would like to add this little fact about translating PHP preg_replace Regex in JavaScript .replace Regex :

<?php preg_replace("/([^0-9\,\.\-])/i";"";"-1 220 025.47 $"); ?>
Result : "-1220025.47"

with PHP, you have to use the quotes "..." around the Regex, a point comma to separate the Regex with the replacement and the brackets are used as a repetition research (witch do not mean the same thing at all.

<script>"-1 220 025.47 $".replace(/[^0-9\,\.\-]/ig,"") </script>
Result : "-1220025.47"

With JavaScript, no quotes around the Regex, a comma to separate Regex with the replacement and you have to use /g option in order to say multiple research in addition of the /i option (that's why /ig).

I hope this will be usefull to someone ! Note that the "\," may be suppressed in case of "1,000.00 $" (English ?) kind of number :

<script>"-1,220,025.47 $".replace(/[^0-9\.\-]/ig,"")</script>
<?php preg_replace("/([^0-9\.\-])/i";"";"-1,220,025.47 $"); ?>
Result : "-1220025.47"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.