Jquery find integer inside string

My text:

var str = 'Cost USD 400.00';

How to use jquery to search for a number on this line only?

+3
source share
3 answers

You should probably not use jQuery; You should use the built-in Javascript regex support. In this case, your regular expression might look like this:

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

The official link to this is: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp .

+6
source

What gillyb said, but I would add:

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

To get anything after the decimal point.

The above RegEx must comply

400

400.01

400,0

400,001

http://xenon.stanford.edu/~xusch/regexp/analyzer.html ... http://www.regular-expressions.info/javascriptexample.html

1, , , 2, , SHOW MATCH.

, .

0

Just find the number using a simple RegEx object in javascript. There is no need for jQuery.

eg:

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

Source: https://habr.com/ru/post/1756029/


All Articles