正则表达式 javascript,字符空格圆括号

RegEx javascript, character whitespace round bracket

本文关键字:空格 圆括号 字符 javascript 正则表达式      更新时间:2023-09-26

我在javascript中有以下字符串:
"哈啰,我是或)"
请注意括号前的空格。我想拥有这个
"你好,我是)"
所以基本上只是删除最后一个 OR。我尝试了所有正则表达式组合都没有成功,即:

string = string .replace(/OR's')/, " )"); //NO
string = string .replace(/OR ')/, " )");  //NO

删除圆括号只有效,但只要我添加 OR,就没有运气

string = string .replace(/')/, " )"); //Removes the bracket
string = string .replace(/'s')/, " )"); //Removes the bracket and the space

我在 http://regexpal.com/尝试了正则表达式/OR''s)/,它运行良好,那么为什么不在javascript中呢?
这感觉像是一个非常简单的问题,但 2 小时后我仍然迷路了。
感谢您的帮助。

string = string.replace(/('s*OR)(?!['s'S]*OR)/,"");

请参阅使用正则表达式替换模式的最后一次出现

这应该有效:

> s = "Hallo OR, I am OR )"
'Hallo OR, I am OR )'
> s.replace(/'bOR['s)]+/g, ')')
'Hallo OR, I am )'