我在JavaScript中有一个字符串(例如,#box2
),我只想从中获取2
。
我尝试过:
var thestring = $(this).attr('href');
var thenum = thestring.replace(/(^.+)(\w\d+\w)(.+$)/i, '$2');
alert(thenum);
它仍然在弹出窗口中返回#box2
。我该怎么做才能让它起作用?
它需要适应结尾附加的任何长度的数字。
我在JavaScript中有一个字符串(例如,#box2
),我只想从中获取2
。
我尝试过:
var thestring = $(this).attr('href');
var thenum = thestring.replace(/(^.+)(\w\d+\w)(.+$)/i, '$2');
alert(thenum);
它仍然在弹出窗口中返回#box2
。我该怎么做才能让它起作用?
它需要适应结尾附加的任何长度的数字。
对于这个具体的例子,
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>
"#box2_col3".replace(/^\D+/g, '')
应该显示 2
,而不是 2_col3
。 - Shiplu Mokaddim#box2_col3
的引用。 - georg#box2_col3
的引用。但是请注意正则表达式OP使用的(\w)(.+$)
),这清楚地表明数字后面还有其他字符。 - Shiplu Mokaddimreplace
函数可以处理带有前导数字的字符串:theString.replace(/^.*\D+/g, '');
,但是 shiplu.mokadd.im 的解决方案更好。 - LandonSchropp您应该尝试以下方法:
var txt = "#div-name-1234-characteristic:561613213213";
var numb = txt.match(/\d/g);
numb = numb.join("");
alert (numb);
1234561613213213
parseInt(txt.match(/\d/g).join(''), 10)
- Lukas Liesis/[+-]?\d+(\.\d+)?/g
。 - Julfikar我认为这个正则表达式可以满足你的需求:
var num = txt.replace(/[^0-9]/g, '');
其中txt
是您的字符串。
它基本上会删除除数字以外的任何内容。
我认为您也可以使用以下方法实现相同的效果:
var num = txt.replace(/\D/g, '');
/[^0-9]/g
为了易读性而加。同时也用于这里大部分答案:https://dev59.com/1HI-5IYBdhLWcg3wbHq- - Avatar请尝试以下代码:string.replace(/[^0-9]/g, '');
这将删除所有非数字字符,仅保留字符串中的数字。
function retnum(str) {
var num = str.replace(/[^0-9]/g, '');
return parseInt(num,10);
}
抱歉,我只能以英文进行回答。console.log('abca12bc45qw'.replace(/[^0-9]/g, ''));
console.log('#box2'.replace(/[^0-9]/g, ''));
/[^0-9]/g
可以替换为 /[^\d]/g
。 - CPHPythonvar thenum = "0a1bbb2".match(/\d+$/)[0];
console.log(thenum);
这是一个提取带有货币符号和格式的价格的代码片段:
var price = "£1,739.12";
parseFloat(price.replace(/[^\d\.]*/g, '')); // 1739.12
我尝试了上一个答案中提到的使用这段代码的所有组合,并使其成功运行。这是唯一在该字符串 (12) 3456-7890 上有效的方法。
var str = "(12) 3456-7890";
str.replace(/\D+/g, '');
结果: "1234567890"
注意:我知道这样的字符串不会在属性中出现,但无论如何,解决方案更好,因为它更加完整。
str.match(/\d+/g).map(Number)
,它将返回一个数组。 - Artem Bernatskyi你可以使用正则表达式从字符串中提取数字:
let string = "xxfdx25y93.34xxd73";
let res = string.replace(/\D/g, "");
console.log(res);
输出:25933473
将其包装到一个纯 JavaScript 函数中:
function onlyNumbers(text){
return text.replace(/\D/g, "");
}
对于像#box2
这样的字符串,应该可以起作用:
var thenum = thestring.replace(/^.*?(\d+).*/,'$1');
jsFiddle: