0

I'm trying to make a RegEx that can single out words from a string but ignore them if they're inside a tag. For example: Even though the searchword is SPAN, do not replace a span tag.

What I have so far is:

(?<![<\/])\bspan\b(?!>)

http://regex101.com/r/vS6yG6

Span obviously is a placeholder. In the script it is generated from a dictionary dynamically.

This is what I'm trying to run:

var reg = new RegExp(the expression, 'gi');

I've escaped the /, so I'm not sure where the problem is.

And this is what I get back: SCRIPT5018: Unexpected quantifier

Any help would be appreciated. I made the Regular Expression with the help of regex101.com.

4
  • possible duplicate of Replace content while ignoring tags Commented Jul 3, 2013 at 10:37
  • Yeah, that's exactelly what I'm doing. but 'span' is added in a loop. I use the expression from the link but escaped '/'. Commented Jul 3, 2013 at 10:39
  • 3
    Negative LookBehind does not exist in javascript. There are workarounds Commented Jul 3, 2013 at 10:40
  • Oh it's that simple? Too bad. Okay, I'll look at the workaround. Thank you! Commented Jul 3, 2013 at 10:44

2 Answers 2

1

Try this ...

/>[^<>]*\b(span)\b[^<>]*<?/ig
Sign up to request clarification or add additional context in comments.

1 Comment

Nope, that selects half the string.
1

Like David replied, there is no Negative LookBehind in Javascript, so that's where the problem was.

http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

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.