0

I have a regex that I run on a text to remove digits before a "." as well as some random characters that may appear after it. This regex matches everything i need it to match which I confirmed in regex101.com. However when I pass it to my javascript function as a parameter to the replaceAll() function, the text returned still has some numbers at the end. After countless hours searching online, i still don't understand why this is happening. Does anyone know the cause?

Regex entered in regex101.com

(^[+-]?\d+(\.|\s|\-|)|(\s\-\.)|(\s\.)?)+(^\d\d|[^\u4E00-\u9FA5|a-zA-Z|\/:«»|0-9|();\n]|\d*[.])

Sample text:

11.君曰
1100.君曰
11.君曰
情奴(新版)
我在未来等着你
11.君曰
11.君曰
11678.君曰
11- -.情海孤舟
11 爱 .情海孤舟

Current output from my js function

updatedText = updatedText.replaceAll(/(^[+-]?\d+(\.|\s|\-|)|(\s\-\.)|(\s\.)?)+(^\d\d|[^\u4E00-\u9FA5|a-zA-Z|\/:«»|0-9|();\n]|\d*[.])/g, "");

output:
君曰
君曰
君曰
情奴(新版)
我在未来等着你
君曰
君曰
君曰
11情海孤舟
11爱情海孤舟

Any help in explaining why the 11's are still in the output would be awesome! Thanks in advance.

D-Money
  • 137
  • 1
  • 12
  • When you are on regex101.com, did you make sure to click ECMAScript (Javascript) on the left side before entering your Regex pattern? Because it defaults to regex for PHP – icecub Jan 21 '22 at 07:49
  • @icecub yes, i made sure that was in fact selected before posting – D-Money Jan 21 '22 at 07:53
  • Not sure what's going on at your side, but your code is working perfectly fine: https://jsfiddle.net/gn3dvm29/ – icecub Jan 21 '22 at 08:01
  • Your regex only works with the `m` modifier. `g` only -> https://regex101.com/r/HMDk2d/1, `gm` -> https://regex101.com/r/JYX4b1/1 – Andreas Jan 21 '22 at 08:02
  • @Andreas Odd.. Seems to work with `g` only perfectly fine on JSFiddle – icecub Jan 21 '22 at 08:04
  • Oh wait.. of course. Multiline! I'm not doing multiline in my example script, that's why it works – icecub Jan 21 '22 at 08:05
  • 1
    See the *All languages - Default flags - Global and Multiline* section in the linked post. – Wiktor Stribiżew Jan 21 '22 at 08:17
  • @Andreas thanks for catching that. I was staring at the expression for hours and didn't think to check modifier. Works like I need it to now! – D-Money Jan 21 '22 at 08:27

0 Answers0