admin管理员组文章数量:1391943
Here are samples:
- example 1:
string1<span id="header">5655811</span>string2
- example 2:
string3<span id="header">51481</span>string4
- example 3:
string5<span id="header">854211</span>string6
How can I get the number between <span id="header">
and </span>
with JavaScript regex?
Here are samples:
- example 1:
string1<span id="header">5655811</span>string2
- example 2:
string3<span id="header">51481</span>string4
- example 3:
string5<span id="header">854211</span>string6
How can I get the number between <span id="header">
and </span>
with JavaScript regex?
- Have you tried anything yet – aaronman Commented Aug 29, 2013 at 16:47
- Aside: Ids should be unique. Use classes instead. – Derek Commented Aug 29, 2013 at 16:51
-
You can't assume from his answer that he's not using IDs uniquely: he's just presenting three examples, they could be on different pages, or different loads of the same page, and the
header
ID could still be unique. Not that it's ever a bad thing to remind people that IDs should be unique. – Ethan Brown Commented Aug 29, 2013 at 16:54
1 Answer
Reset to default 5It depends on how safe you want to be. If you're sure that all of your input looks exactly like you have in your message, you can use:
var n = Number( input.match( /<span id="header">(\d+)<\/span>/ )[1] );
A slightly safer version would be:
var n;
var match = input.match( /<span id="header">\s*(\d+)\s*<\/span>/ );
if( match ) {
n = Number( match[1] );
} else {
// do error handling here
}
本文标签: regexRegular Expression to get a number between two strings in JavascriptStack Overflow
版权声明:本文标题:regex - Regular Expression to get a number between two strings in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744746160a2622892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论