I want extract an Id from the string <p>id = 22<p>\\n<p>1. iteration, 1. task<p>
<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?
()
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]
/<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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.