Forum Moderators: open

Message Too Old, No Replies

Regex

         

mehh

7:12 pm on Mar 10, 2007 (gmt 0)

10+ Year Member



I'm no good with regex. can someone help me create one that will find two forward slashes outside a pair of single or double quotes. thanks

eelixduppy

3:53 am on Mar 11, 2007 (gmt 0)



What do you mean by "outside"? Could you give a more specific example, please?

mehh

2:51 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



so:
hello "//", would fail but,
hello // , would pass.

i *think* it should look like this:
/(\/\/([^"\']*)$)/

but if the string value was
hello // this isn't in quote marks, it still fails even though it isn't in quote marks.

RonPK

10:21 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you're looking for string A (//) that is not preceeded by string B ("). Unfortunately JavaScript does not support lookbehinds in regular expressions (lookaheads are supported). If this were Perl, you could try a pattern like
/(?<=")\/\//
. In JavaScript it will throw a compilation error.

Me thinks you'll have to do this in two steps.

RonPK

8:45 am on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(wakes up, slaps forehead) Here's a smarter pattern:
/(^¦[^"'])\/\/([^"']¦$)/

It returns a match for two forward slashes that are
- preceeded by either start of line or any character that is not a quote character
AND
- followed by either end of line or any character that is not a quote character

Only downside I see is that it will also match a string like

hello '//"
. That can be fixed by making separate patterns for single and double quotes:
/((^¦[^'])\/\/([^']¦$)¦(^¦[^"])\/\/([^"]¦$))/

(replace the broken pipes ¦ by solid ones)