ricardoespsanto
ricardoespsanto

Reputation: 1110

Remove comma from group regex

Is it possible from this:

US Patent 6,570,557

retrieve 3 groups being:

  1. US
  2. Patent
  3. 6570557 (without the commas)

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

Answers (4)

Binary Brain
Binary Brain

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

Dave Sexton
Dave Sexton

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

hsz
hsz

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

Jon
Jon

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

Related Questions