3 回答
TA贡献1868条经验 获得超4个赞
var thenum = thestring.replace( /^\D+/g, ''); // replace all leading non-digits with nothing
thenum = "foo3bar5".match(/\d+/)[0] // "3"
function getre(str, num) {
if(str === num) return 'nice try';
var res = [/^\D+/g,/\D+$/g,/^\D+|\D+$/g,/\D+/g,/\D.*/g, /.*\D/g,/^\D+|\D.*$/g,/.*\D(?=\d)|\D+$/g];
for(var i = 0; i < res.length; i++)
if(str.replace(res[i], '') === num)
return 'num = str.replace(/' + res[i].source + '/g, "")';
return 'no idea';
};
function update() {
$ = function(x) { return document.getElementById(x) };
var re = getre($('str').value, $('num').value);
$('re').innerHTML = 'Numex speaks: <code>' + re + '</code>';
}
<p>Hi, I'm Numex, the Number Extractor Oracle.
<p>What is your string? <input id="str" value="42abc"></p>
<p>What number do you want to extract? <input id="num" value="42"></p>
<p><button onclick="update()">Insert Coin</button></p>
<p id="re"></p>
TA贡献1802条经验 获得超5个赞
var txt = "#div-name-1234-characteristic:561613213213";var numb = txt.match(/\d/g);numb = numb.join("");alert (numb);
1234561613213213
TA贡献1810条经验 获得超4个赞
var num = txt.replace(/[^0-9]/g,'');
txt
var num = txt.replace(/\D/g,'');
添加回答
举报