Why doesn't this regular expression work in JavaScript? /(?i)-StringA$|-StringB$/
P粉314915922
P粉314915922 2023-09-10 10:32:13
0
1
516

Built this regex in the generator and everything worked as expected, tried cleaning some strings in my app and the app said it was invalid.

The statement is as follows:

const reg = /(?i)-TeamMember$|-TeamLead$/; testString = testString.replace(reg, "");

Trying to run the application results in the following error:

模块解析失败:无效的正则表达式:/(?i)-StringA$|-StringB$/: 无效的分组(199:21) 文件已使用以下加载器进行处理: * ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js * ./node_modules/@ngtools/webpack/src/ivy/index.js 您可能需要额外的加载器来处理这些加载器的结果。

Tried this in the generator and everything works as expected, the goal is to remove the suffix from the string if it matches any of the provided strings and ignore case.

P粉314915922
P粉314915922

reply all (1)
P粉562845941

JavaScript has no built-in case-insensitive inline flag. Instead, you should use the case-insensitive flag/i

const reg = /-TeamMember$|-TeamLead$/i; testString = testString.replace(reg, "");

You can read more here:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!