1

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?

2
  • 2
    What is the regex you've tried? Commented Jul 9, 2012 at 14:44
  • 1
    Is it the only number you'll ever have? Commented Jul 9, 2012 at 14:44

4 Answers 4

7

This will get all the numbers:

var myValue = /\d+/.exec(myString)
Sign up to request clarification or add additional context in comments.

1 Comment

If you want an array of more than one match use myString.match(/\d+/g)
4
mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"

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

2 Comments

Thanks, I'm using it now. But if there is no number, how do I handle the error? I have: Evaluator.evaluateString() problem
Do var match = mystr.match(/assignment_group=([^\^]+)/), result = match ? match[1] : ''; to avoid the error and get an empty string instead.
1

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);

Comments

0

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"

2 Comments

The regex I've trid was a simple \bassignment_group. I've managed to find another way, by checking the position, and using substring ... it works. But I thought it could have been a nice way to learn regex !
well if you're trying to learn regex, I'd suggest the tutorial on w3schools.org, though I normally don't recommend that site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.