Extracting numbers from a string using regular expressions

后端 未结 4 2084
北恋
北恋 2021-01-25 10:22

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 thi

相关标签:
4条回答
  • mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"
    

    This will find everything from the end of "assignment_group=" up to the next caret ^ symbol.

    0 讨论(0)
  • 2021-01-25 10:59

    This will get all the numbers:

    var myValue = /\d+/.exec(myString)
    
    0 讨论(0)
  • 2021-01-25 11:02

    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"

    0 讨论(0)
  • 2021-01-25 11:07

    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);
    
    0 讨论(0)
提交回复
热议问题