Why this regex does not work with Eastern Arabic numerals?

后端 未结 1 748
无人共我
无人共我 2021-01-19 00:55

@thg435 wrote this answer to a javascript question:

> a = \"foo 1234567890 bbb 123456\"
\"foo 1234567890 bbb 123456\"
> a.replace(/\\d(?=\\d\\d(\\d{3})         


        
1条回答
  •  梦毁少年i
    2021-01-19 01:23

    Oddly, I'm experiencing the opposite behavior from you (the first one doesn't work and the other two do), but how about if you replaced the \b with (?![\u0660-\u0669])? Then it seems to work no matter what's before or after it:

    [\u0660-\u0669](?=[\u0660-\u0669][\u0660-\u0669]([\u0660-\u0669]{3})*(?![\u0660-\u0669]))
    

    Edit: This seems to work for the new requirement - to only add the brackets if the run of digits is 3 digits long or more:

    [\u0660-\u0669](?=[\u0660-\u0669]{2}([\u0660-\u0669]{3})+(?![\u0660-\u0669]))|(?<=[\u0660-\u0669]{2})[\u0660-\u0669](?=[\u0660-\u0669]{2}(?![\u0660-\u0669]))
    

    Incidentally, some Regex processors will treat those digits as a match for \d. Here is that second Regex with \d instead of those character ranges, which should be a little easier to read:

    \d(?=\d{2}(\d{3})+(?!\d))|(?<=\d{2})\d(?=\d{2}(?!\d))
    

    0 讨论(0)
提交回复
热议问题