You can get it like this:
var suffix = 'comment_like_123456'.match(/\d+/); // 123456
With respect to button:
$('.comment_like').click(function(){
var suffix = this.id.match(/\d+/); // 123456
});
Answer from Sarfraz on Stack OverflowYou can get it like this:
var suffix = 'comment_like_123456'.match(/\d+/); // 123456
With respect to button:
$('.comment_like').click(function(){
var suffix = this.id.match(/\d+/); // 123456
});
You can try this. it will extract all number from any type of string.
var suffix = 'comment_like_6846511';
alert(suffix.replace(/[^0-9]/g,''));
DEMO
parseInt() is pretty sweet.
HTML
<span id="foo">280ms</span>
JS
var text = $('#foo').text();
var number = parseInt(text, 10);
alert(number);
parseInt() will process any string as a number and stop when it reaches a non-numeric character. In this case the m in 280ms. After have found the digits 2, 8, and 0, evaluates those digits as base 10 (that second argument) and returns the number value 280. Note this is an actual number and not a string.
Edit:
@Alex Wayne's comment.
Just filter out the non numeric characters first.
parseInt('ms120'.replace(/[^0-9\.]/g, ''), 10);
Try this:
var num = document.getElementById('spanID').innerText.match(/\d+/)[0];
jQuery version:
var num = $('span').text().match(/\d+/)[0]; // or $('#spanID') to get to the span
If you want as numeric value (and not as string), use parseInt:
var num = parseInt($('span').text().match(/\d+/)[0], 10);
Yes, match is the way to go:
var matches = str.match(/(\d+)sl(\d+)/);
var number1 = Number(matches[1]);
var number2 = Number(matches[2]);
If the string is always going to look like this: "ch[num1]sl[num2]", you can easily get the numbers without a regex like so:
var numbers = str.substr(2).split('sl');
//chop off leading ch---/\ /\-- use sl to split the string into 2 parts.
In the case of "ch2sl4", numbers will look like this: ["2", "4"], coerce them to numbers like so: var num1 = +(numbers[0]), or numbers.map(function(a){ return +(a);}.
If the string parts are variable, this does it all in one fell swoop:
var str = 'ch2fsl4';
var numbers = str.match(/[0-9]+/g).map(function(n)
{//just coerce to numbers
return +(n);
});
console.log(numbers);//[2,4]
This will get all the numbers separated by coma:
var str = "10 is smaller than 11 but greater then 9"; var pattern = /[0-9]+/g; var matches = str.match(pattern);
After execution, the string matches will have values "10,11,9"
If You are just looking for thew first occurrence, the pattern will be /[0-9]+/ - which will return 10
(There is no need for JQuery)
This uses regular expressions and the exec method:
var s = "blabla blabla-5 amount-10 blabla direction-left";
var amount = parseInt(/amount-(\d+)/.exec(s)[1], 10);
var direction = /direction-([^\s]+)/.exec(s)[1];
The code will cause an error if the amount or direction is missing; if this is possible, check if the result of exec is non-null before indexing into the array that should be returned.
You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /(\(\d*\)|\D)/g
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/(\(\d*\)|\D)/g, '');
console.log(telnom); //05343615
If your phone numbers always start with something like Tel:, Tel(1): etc you could, for example, consider simply splitting the whole string first using : (or a space) as separators. Like this:
telnom = dine[i].tel1;
telnom = telnom.split(":")[1].replace(/\D/g, '');
I think a RegExp would be a good idea:
var sd = $(this).text().replace(/[^0-9]/gi, ''); // Replace everything that is not a number with nothing
var number = parseInt(sd, 10); // Always hand in the correct base since 010 != 10 in js
You can use parseInt for this, it will parse a string and remove any "junk" in it and return an integer.
As James Allardice noticed, the number must be before the string. So if it's the first thing in the text, it will work, else it won't.
-- EDIT -- Use with your example:
<p>123confirm</p>
<script type="text/javascript">
$(document).ready(function(){
$('p').click(function(){
var sd=$(this).text();
sd=parseInt(sd);
alert(sd);
});
});
</script>
Why not just use slice() or substring()?
var tier = this.id.slice(6);
// -> 1, 2, 3... 11... 123, etc
Example - http://jsfiddle.net/TmBQ8/
PS, you're getting null at the moment because you're passing a string argument to match, instead of a regular expression. Remove the quotes, e.g. match(/\d+$/). Also note in my example, I skipped using a jQuery wrapper and attr() since it's the long way around and not as efficient as direct property access.
If your id always has "record" in front of it then...
$("a.removeTier").live('click', function() {
var tier = $(this).attr('id').subString("6");
alert(tier);
});
If you change the word "record" to something else just change the 6 the the position of the first number.
Once you get you string, your can replace every non digit character and the dot :
var number = '$ 4.5'.replace(/[^\d\.]/g, '');
Then you can parse it :
number = parseFloat(number);
//Alternatively
number = +number;
I aproached your problem by using JQuery to replace any character that isnt a number or a '.' (dot).
I created a few spans with values in like this:
<span>$4.10</span>
<span>£7.76</span>
<span>€23.44</span>
then created some JQuery to alert the values:
$("span").each(function(){
var a = $(this).text().replace(/[^0-9.]/g, "");
alert(a);
});
Here is a JSFiddle you can mess with: http://jsbin.com/xelameki/1/edit/
I hope it helps =)
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.
You could use the following to pull out just the decimals:
function extractNumber(string2extract){
var numberPattern = /[0-9]+(\.[0-9][0-9]?)?/g;
return string2extract.match(numberPattern);
}
var PriceReftxt = extractNumber($('.Price').text());
var SRPReftxt = extractNumber($('.SRPRef').text());
RegEx perhaps?
$('.Price').text().replace(/[^0-9]/gi, '');
var number = parseInt(Price, 10);
$('.SRPRef').text().replace(/[^0-9]/gi, '');
var number2 = parseInt(SRPRef, 10);