用一个替换多个连续的连字符 [英] replace multiple consecutive hyphens with one

查看:43
本文介绍了用一个替换多个连续的连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Javascript可以实现吗?

Is this possible in Javascript?

我来自Java,但无法在Javascript中使用.

I have this from Java, but can not be used in Javascript.

s/-{2,}/-/g

还有另一种可行的方法吗?

Is there another way that works?

推荐答案

是的.您可以将同一正则表达式与Javascript replace() 方法.

Yes it is. You can use the same regex with the Javascript replace() method.

s.replace(find, replacement)
// where 's' is your string object

示例:

var r = 'foo--bar---baz'.replace(/-{2,}/g, '-');
console.log(r); // "foo-bar-baz"

这篇关于用一个替换多个连续的连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆