Reputation: 1110
Is it possible from this:
US Patent 6,570,557
retrieve 3 groups being:
So far I got:
(US)(\s{1}Patent\s{1})(\d{1},\d{3},\d{3})
and was trying (?!,)
to get rid of the commas then I effectively get rid of the whole number.
Upvotes: 5
Views: 1720
Reputation: 1211
Instead of using regex, you can do it with 2 simple functions:
var str = "US Patent 6,570,557"; // Your input
var array = str.split(" "); // Separating each word
array[2] = array[2].replace(",", ""); // Removing commas
return array; // The output
This should be faster too.
Upvotes: 2
Reputation: 11188
Just add more groups like so:
(US)(\s{1}Patent\s{1})(\d{1}),(\d{3}),(\d{3})
And then concatenate the last 3 groups
Upvotes: 0
Reputation: 152206
Try with:
var input = 'US Patent 6,570,557',
matches = input.match(/^(\w+) (\w+) ([\d,]+)/),
code = matches[1],
name = matches[2],
numb = matches[3].replace(/,/g,'');
Upvotes: 10
Reputation: 437336
You cannot ignore the commas when matching, unless you match the number as three separate parts and then join them together.
It would be much preferable to strip the delimiters from the number from the matching results with String.replace
.
Upvotes: 1