For this specific example,
var thenum = thestring.replace(/^\D+/g, ''); // Replace all leading non-digits with nothing
In the general case:
thenum = "foo3bar5".match(/\d+/)[0] // "3"
Here's a bonus: regex generator.
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>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
For this specific example,
var thenum = thestring.replace(/^\D+/g, ''); // Replace all leading non-digits with nothing
In the general case:
thenum = "foo3bar5".match(/\d+/)[0] // "3"
Here's a bonus: regex generator.
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>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You should try the following:
var txt = "#div-name-1234-characteristic:561613213213";
var numb = txt.match(/\d/g);
numb = numb.join("");
console.log(numb);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Extract numbers from a string-Javascript - Code Review Stack Exchange
How to remove numbers from end of the string
Extract only numbers from string ?
It will match twice because you are grouping the number with parenthesis. This should work: https://regex101.com/r/d2uvuy/3
[-+]?[0-9]+
How to get keys from enum without converting to any?
I'm not really sure I understand, but isn't Object.keys(ShoeType) what you want?
I used the following regexp to extract numbers from a string:
var string = "border-radius:10px 20px 30px 40px";
var numbers = string.match(/\d+/g).map(Number);
The code will fail in below cases:
- Negative numbers
- Decimal numbers
- Units other than
px(e.g.pt,%,vw,vh, ...)
Moreover, the numbers array contains space before numbers and an empty string at the end which is not required.
I recommend to use below regex to extract numbers from strings
/[+-]?\d+(?:\.\d+)?/g
[+-]?: Optional+or-sign before number\d+: Match one or more numbers(?:\.\d+)?: Optional decimal point.?:denotes non-capturing group.gflag: To get all matches
After the numbers are extracted from string, they can be converted to Number format.
var regex = /[+-]?\d+(?:\.\d+)?/g;
var str = `padding: 0;
font-size: 16pt;
width: 50%;
height: 20vh;
margin-right: 12px;
padding-right: -12.5px;`;
var match;
while (match = regex.exec(str)) {
console.log(match[0]);
}
Here's online demo of the regex on Regex101.