Is it possible from this:
US Patent 6,570,557
retrieve 3 groups being:
- US
- Patent
- 6570557 (without the mas)
So far I got:
(US)(\s{1}Patent\s{1})(\d{1},\d{3},\d{3})
and was trying (?!,)
to get rid of the mas then I effectively get rid of the whole number.
Is it possible from this:
US Patent 6,570,557
retrieve 3 groups being:
- US
- Patent
- 6570557 (without the mas)
So far I got:
(US)(\s{1}Patent\s{1})(\d{1},\d{3},\d{3})
and was trying (?!,)
to get rid of the mas then I effectively get rid of the whole number.
-
Why not just retrieve the number with the mas, and then strip the mas when you need to?
/(US)(\sPatent\s)([\d,]*)/
Lazy ftw! – kojiro Commented Jan 22, 2013 at 13:49 - I forgot to say I can't Change the JS I was trying to achieve this in regex only – ricardoespsanto Commented Jan 22, 2013 at 14:04
- The regex is not in the JS? – kojiro Commented Jan 22, 2013 at 15:34
- well no... its retrieved through a configuration mechanism outside the js – ricardoespsanto Commented Jan 22, 2013 at 15:41
4 Answers
Reset to default 10Try 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,'');
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 mas
return array; // The output
This should be faster too.
You cannot ignore the mas 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
.
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