最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove comma from group regex - Stack Overflow

programmeradmin8浏览0评论

Is it possible from this:

US Patent 6,570,557

retrieve 3 groups being:

  1. US
  2. Patent
  3. 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:

  1. US
  2. Patent
  3. 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.

Share Improve this question edited Jan 22, 2013 at 13:56 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Jan 22, 2013 at 13:44 ricardoespsantoricardoespsanto 1,11011 silver badges34 bronze badges 4
  • 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
Add a ment  | 

4 Answers 4

Reset to default 10

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

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

发布评论

评论列表(0)

  1. 暂无评论