0
var str = "<example>{{var=b|arg=args|link=c|testing=test1}}</example>";

How do I use regex to match args? In other words, I want to match the thing that follows arg= but before the next |.

2
  • Rebeca, what you already tried? Commented Sep 3, 2012 at 22:09
  • I hope you're not trying to perform any sort of XML or HTML parsing with regex. Commented Sep 3, 2012 at 22:10

2 Answers 2

3
var match = str.match(/arg=([^|]+)/);

Then check if match[1] exists. And if it does - then it contains what you want

UPD:

as @nnnnnn pointed out - instead of checking for match[1] presence it would be more correct to check if match is not null like:

if (match) {
    // match[1] here contains required info
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that checking match[1] will give an error if there was no match: you have to test if match is null...
1

Something like this:

var args = str.match(/arg=([^|]*)/);
if (args != null) {
   // args[1] contains the match...
}

That is, find arg= and then capture the zero or more following characters that aren't |.

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.