1

I would like to get special formatted strings ({string}) out of the HTML which are not inside a specific HTML tag.

For example I would like to match {test} and not <var>{test}</var>.

Therefore I am using the following regex: (excluding is done with ?!)

(?!<var>)\{\S+?\}(?!<\/var>)

So this works very well for texts with spaces, but if I have something like (where there is no space in-between):

<var>{name}</var>{username} 

it matches two {}-strings: {name}</var>{username}

How can I just match {username} here?

Update: If I need to do something like this

<var.*?<\/var>|(\{\S+?\})

How can I get the matched values, because the matched index depends on the position.

Examples:

Match 1:

"{username}<var>{name}</var>".match(/<var.*?<\/var>|(\{\S+?\})/g);
=> ["{username}", "<var>{name}</var>"]

Match 2:

"<var>{name}</var>{username}".match(/<var.*?<\/var>|(\{\S+?\})/g);
=> ["<var>{name}</var>", "{username}"]

Current Solution:

angular.forEach(html.match(regex), function (match) {
  if(match.substring(0, 4) !== '<var') {
    newAdded = match;
  }
});

Is this really the 'best' solution for JavaScript?

Betty St
  • 2,741
  • 21
  • 33
  • 1
    I think this one is better: [*Match text not inside span tags*](http://stackoverflow.com/questions/24356167/match-text-not-inside-span-tags). – Wiktor Stribiżew Aug 19 '15 at 07:59
  • yes this one is better then the duplicated question! One question to that answer: Do you know how I can use the matched value, because the position depends on where it is found, either: http://www.rubular.com/r/RKVJSGIsvQ or http://www.rubular.com/r/qK5wYnlJCR – Betty St Aug 19 '15 at 08:03
  • You need to check if the captured group is `!== undefined` and add the corresponding logic. – Wiktor Stribiżew Aug 19 '15 at 08:16
  • Thank you for your answer, I updated my question, because in javascript I also get the matches with HTML tag in the result. – Betty St Aug 19 '15 at 08:19
  • When 'RegEx match open tags except XHTML self-contained tags' is your first related question, you should worry: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?rq=1 – Mario Trucco Aug 19 '15 at 09:59
  • just care about using regexp to parse HTML ;) http://stackoverflow.com/a/1732454/2835089 – arnaudbey Aug 19 '15 at 12:31
  • @arnaudbey yes but this is a special case, I know exactly how the tag and its content look like. So I think it is safe to use regex for that! – Betty St Aug 19 '15 at 13:36

1 Answers1

3

Here is how you can achieve this using the following regex:

/<var.*?<\/var>|(\{\S+?\})/g;

var s = '<var>{name}</var>{username}<var>{newname}</var>{another_username}';
var log = [];
var m;
var regex = /<var.*?<\/var>|(\{\S+?\})/g;
while ((m = regex.exec(s)) !== null) {
   if ( m[1] !== undefined) {
     log.push(m[1]);
   }
}
alert(log);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    thank you :) I trimmed it a little bit: ``while ((m = regex.exec(s))) { if (m[1]) { log.push(m[1]); } }`` – Betty St Aug 19 '15 at 11:21