Don't match a group if it starts with a string in javascript
I'm struggling with some regex, in javascript which doesn't have a typical lookbehind option, to only match a group if it's not preceded with a string:
(^|)(www.[S]+?(?= |[,;:!?]|.( )|$))
so in the following
hello http:/www.mytestwebsite.com is awesome
I'm trying to detect if the www.mytestwebsite.com is preceeded by
/
and if it is I don't want to match, otherwise match away. I tried using a look ahead but it looked to be conflicting with the look ahead I already had.
I've been playing around with placing (?!/) in different areas with no success.
(^|)((?!/)www.[S]+?(?= |[,;:!?]|.( )|$))
A look ahead to not match if the match is preceded
Due to lack of lookbehinds in JS, the only way to accomplish your goal
is to match those web sites that contain the errant /
as well.
This is because a lookahead won't advance the current position.
Only a match on consumable text will advance the position.
But, a good workaround has always been to include the errant text as an option
within the regex. You'd put some capture groups around it, then test the
group for a match. If it matched, skip, go on to next match.
This requires sitting in a while loop checking each successful match.
In the below regex, if group 1 matched, don't store the group 2 url,
If it didn't, store the group 2 url.
(/)?(www.S+?(?= |[,;:!?]|.( )|$))
Formatted:
( / )? # (1)
( # (2 start)
www. S+?
(?=
 
| [,;:!?]
| .
(   ) # (3)
| $
)
) # (2 end)
Another option (and I've done zero performance testing) would be to use string.replace()
with a regex and a callback as the second parameter.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Then, inside of the replace function, prepend/append the illegal/characters you don't want to match to the matched string, using the offset parameter passed to the callback (see above docs) you can determine each match, and it's position and make a determination whether to replace the text or not.
链接地址: http://www.djcxy.com/p/12980.html上一篇: Ruby正则表达式指定捕获组的长度