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]
There is a difference between Java and JavaScript regex flavors: JS does not support lookbehind. A tabulation of differences between regex flavors can be found on Wikipedia. However, this does not apply to your case. I surmise that your JS test string has spurious characters, eg.
In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers. $exp = "/w3schools/i"; In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.
RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.
* means zero-or-more, and + means one-or-more. So the difference is that the empty string would match the second expression but not the first.
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 anx
.[^]x]
matches any character that is not a closing bracket or anx
. 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:
(?1)
regex subroutine used to shorten a PCRE pattern conversion - REGEX from PHP to JS
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"
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