简体   繁体   中英

Extracting numbers from a string using regular expressions

I am clueless about regular expressions, but I know that they're the right tool for what I'm trying to do here: I'm trying to extract a numerical value from a string like this one:

approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^

Ideally, I'd extract the following from it: 12345678901234567890123456789012 None of the regexes I've tried have worked. How can I get the value I want from this string?

这将得到所有数字:

var myValue = /\d+/.exec(myString)
mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"

这将找到从“ assignment_group =“的末尾到下一个插入符号^

Try something like this:

/\^assignment_group=(\d*)\^/

This will get the number for assignment_group .

var str = 'approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^',
    regex = /\^assignment_group=(\d*)\^/,
    matches = str.match(regex),
    id = matches !== null ? matches[1] : '';
console.log(id);

If there is no chance of there being numbers anywhere but when you need them, you could just do:

\d+

the \\d matches digits, and the + says "match any number of whatever this follows"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM