0

I want extract an Id from the string <p>id = 22<p>\n<p>1. iteration, 1. task<p>

with:

var str="<p>id = 22<p>\n<p>1. iteration, 1. task<p>";
var patt1=/<p>id = (.*)<p>/;
document.write(str.match(patt1));

but somehow it returns to the browser:

id = 22 
,22

Why so? Why 2 matches?

2 Answers 2

2

() is a grouping operator useful for extracting portions of the match. If you just want to extract the id then use str.match(patt1)[1]

Sign up to request clarification or add additional context in comments.

1 Comment

This will cause a exception if no match is found
0
/<p>id[^<]+<p>/

Seems to be a better fit for your task. But your input string is very confusing. Shouldn't it be some HTML open-closed tags sequence?

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.