Here are samples:
string1<span id="header">5655811</span>string2string3<span id="header">51481</span>string4string5<span id="header">854211</span>string6How can I get the number between <span id="header"> and </span> with JavaScript regex?
It 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With