-1

In the string "test <test> test </test> test" i want to match only these occurrences of "es":

"test <test> test </test> test"

Anyone knows how to do this?

This is not the same as as the possible duplicate Regex replace text outside html tags where the result would be

"test <test> test </test> test", as the inner "es" is not getting matched

Update: forgot to mention that <test> should be replaceable with <any stuff="test">...</any> or <testing some="stuff">...</testing>.

JavaScript regex flavor.

Community
  • 1
  • 1
Daniel F
  • 13,684
  • 11
  • 87
  • 116
  • 3
    Possible duplicate of [Regex replace text outside html tags](http://stackoverflow.com/questions/18621568/regex-replace-text-outside-html-tags) – dakab Nov 11 '15 at 15:38
  • @dakab no, not the same. I now added an explanation. (but I'm upvoting the comment, as it is a helpful reference) – Daniel F Nov 11 '15 at 15:47
  • What language and what regex did you try? – bobble bubble Nov 11 '15 at 15:51
  • @bobblebubble The "JavaScript (Chrome)" mode in RegexBuddy. I really didn't get to try, I thought about it, tried some stuff with lazy matching, but gave up. – Daniel F Nov 11 '15 at 18:18

3 Answers3

1

If it's for PCRE you can skip the tags

<[^>]*>(*SKIP)(*F)|es

< matches literally, followed by negated class [^>]* any amount of character, that are not > followed by literal > and is skipped | or es is matched in the remaining text.

Demo at regex101. Or use a lookahead

es(?![^<]*>)

After matching es literally (?! starts a negative lookahead to check if it's not before a closing > with no < in between. If there's NOT any amount of non< followed by literal > ahead.

See demo at regex101

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
0

You can try this: DEMO

<test>|<\/test>|(es)

which matches tags, but only capture the non-tags.

ergonaut
  • 6,929
  • 1
  • 17
  • 47
  • Could you modify this for any tag, even tags with attributes in them? (Sorry I had to unapprove the answer, noticed to late that it was limited to the test string. will update the question) – Daniel F Nov 11 '15 at 18:24
  • Have you not heard on SO that you should not use regex in the general case but use a proper parser? http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – ergonaut Nov 11 '15 at 18:34
  • This is what regex was made for. I mean, this matching through simple logic. No grammar required for this task. – Daniel F Nov 12 '15 at 08:24
0

Try /(es)(?=[^>]*(?:<|$))/g yourself or view it in the tester.

It uses a positive look-ahead to ensure that anything other than a closing angle bracket follows, right to the next opening angle bracket or the end of the string.

Note that look-around assertions in particular have quite varying support, depending on the flavor you are using. You might get lucky with look-ahead, however.

dakab
  • 5,379
  • 9
  • 43
  • 67
  • If I could, I would also accept this one. I'm accepting "bobble bubble"'s one because it's shorter. But thanks, and congrats. – Daniel F Nov 11 '15 at 18:31
  • While the accepted answer might be the best solution provided, shortness (or verboseness) shouldn’t be the only criterion. Thanks anyway for the consideration. – dakab Nov 11 '15 at 18:43