Regular expressions:
var numberPattern = /\d+/g;
'something102asdfkj1948948'.match( numberPattern )
This would return an Array with two elements inside, '102' and '1948948'. Operate as you wish. If it doesn't match any it will return null.
To concatenate them:
'something102asdfkj1948948'.match( numberPattern ).join('')
Assuming you're not dealing with complex decimals, this should suffice I suppose.
Answer from meder omuraliev on Stack OverflowRegular expressions:
var numberPattern = /\d+/g;
'something102asdfkj1948948'.match( numberPattern )
This would return an Array with two elements inside, '102' and '1948948'. Operate as you wish. If it doesn't match any it will return null.
To concatenate them:
'something102asdfkj1948948'.match( numberPattern ).join('')
Assuming you're not dealing with complex decimals, this should suffice I suppose.
You could also strip all the non-digit characters (\D or [^0-9]):
let word_With_Numbers = 'abc123c def4567hij89'
let numbers = word_With_Numbers.replace(/\D/g, '');
console.log(numbers)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
javascript - Regex to check whether a string contains only numbers - Stack Overflow
javascript - Return only numbers from string - Stack Overflow
regex - How to get only number in javascript? - Stack Overflow
regex - Javascript regular expression to allow only numbers upto 10 digits, string and special charecters are not allowed - Stack Overflow
This is a great use for a regular expression.
var str = "Rs. 6,67,000";
var res = str.replace(/\D/g, "");
alert(res); // 667000
\D matches a character that is not a numerical digit. So any non digit is replaced by an empty string. The result is only the digits in a string.
The g at the end of the regular expression literal is for "global" meaning that it replaces all matches, and not just the first.
This approach will work for a variety of input formats, so if that "Rs." becomes something else later, this code won't break.
For this task the easiest way to do it will be to us regex :)
var input = "Rs. 6,67,000";
var res = input.replace(/\D/g,'');
console.log(res); // 667000
Here you can find more information about how to use regex:
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
I hope it helped :)
Regards
You can just get all the numbers from the string, like this
console.log('$75 - $300'.match(/(\d+)/g));
# [ '75', '300' ]
Note: This simple RegEx will match and get all the numbers, even if there are more than 2 numbers in the string. But for this simple task, you don't need a complicate RegEx.
If you want to fix your program, you can do it like this
console.log('$75 - $300'.replace(/\$/g, '').split(/\s*-\s*/));
# [ '75', '300' ]
replace(/\$/g, '') will replace $ symbol from the string and then you can split the string based on \s*-\s*, which means zero or more white space characters, followed by -, and followed by zero or more white space characters.
You could extend the regular expression in the .replace() function to have a global flag g:
str.replace(/\$/g,'');
The whole script would work like this, but will be specific to removing the $ sign.
function price_range(id){
//var str = $('#'+id).val();
var str = '$75 - $300';
var removeDollar = str.replace(/\$/g,'');
alert(removeDollar);
}
<a href="javascript:;" onclick="price_range('amount')">Click Here</a>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Try using this regex:
var compare=/^[0-9]{1,10}$/g
Use [0-9]d{10} to specify only characters from 0-9 and 10 of them.
<!DOCTYPE html>
<html>
<body>
<input type="text" id="num" onblur="myFunction()">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script>
function myFunction() {
var number=$("#num").val();
var compare=/[0-9]{10}/;
if(number.match(compare)){
alert('match');
}
else{
alert('not match');
}
}
</script>
</body>
</html>
Or, better yet, do it completely with HTML:
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" id="num" pattern="[0-9]{10}" required><button>test</button>
</form>
</body>
</html>
Is there a regular expression to check for only numerical characters and no blanks? The furthest I’m able to get is this: [0-9]*$ Unfortunately it doesn’t account for blanks. These didn’t work for me either: \d+$ /-?\d+$/
I’m using this regex to confirm a SharePoint field (formatted as single line of text) contains only numbers even though some inputs are alphanumeric or blank.