什么似乎是正确的正则表达式在替换函数中不起作用(What seems to be a correct regex doesn't work in replace function)

编程入门 行业动态 更新时间:2024-10-28 11:32:59
什么似乎是正确的正则表达式在替换函数中不起作用(What seems to be a correct regex doesn't work in replace function)

我试图匹配三个连续的点(“。”),然后选择一个空格。

我的想法如下:

\.\.\.\s?

在这里进行了测试 ,似乎完全符合预期。

但是当我尝试在javascript的替换函数中使用它时它似乎不起作用,除非我遗漏了一些愚蠢的东西,否则它很奇怪:

replace("\.\.\.\s?", "")

不起作用,请看这里的现场演示 。

我错过了什么?

I am trying to match three consecutive dots (".") followed optionally by a space.

My idea was the following:

\.\.\.\s?

Tested it here and seems to do exactly as expected.

But then when I try to use it in the replace function with javascript it doesn't seem to work, it's quite weird unless I'm missing something silly:

replace("\.\.\.\s?", "")

Doesn't work, see live demo here.

What am I missing?

最满意答案

正则表达式不应该在引号中。 尝试...

mystr.replace(/\.\.\.\s?/, "")

的jsfiddle

The regex shouldn't be in quotes. Try...

mystr.replace(/\.\.\.\s?/, "")

jsfiddle

更多推荐

本文发布于:2023-08-01 19:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1363611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:似乎是   中不   函数   正确   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!