开发者

jquery find integer inside string

开发者 https://www.devze.com 2023-01-08 23:42 出处:网络
My text: var str = \'Co开发者_如何学Pythonst USD 400.00\'; How do i use jquery to find number only in that str?You probably shouldn\'t use JQuery for that; you should use built-in Javascript regula

My text:

var str = 'Co开发者_如何学Pythonst USD 400.00';

How do i use jquery to find number only in that str?


You probably shouldn't use JQuery for that; you should use built-in Javascript regular expression support. In this case your regular expression might look like:

var result = /\d+(?:\.\d+)?/.exec(mystring);

The official reference for this is at https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp .


What gillyb said but I'd just add:

var str=new RegExp("\\d+\.?\d+?");

To pick up anything after the decimal point.

The above RegEx should match

400

400.01

400.0

400.001

I often use http://xenon.stanford.edu/~xusch/regexp/analyzer.html when building regular expressions... but you also might find this one very useful http://www.regular-expressions.info/javascriptexample.html

if you put the regex on line one, the string your testing against on line 2 then you can see what gets put into the variable with the SHOW MATCH button.

Try it with variations of the above numbers and see what comes back.


Just search for the number using a plain RegEx object in javascript. No need for jquery here.

e.g :

var str = new RegExp("\\d+(?:\\.\\d+)");
var num = str.exec("Cost USD 400.00");
0

精彩评论

暂无评论...
验证码 换一张
取 消