正则表达式用短划线替换空格 [英] Regular expression to replace spaces with dashes

查看:162
本文介绍了正则表达式用短划线替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出我需要取值的正则表达式并用短划线替换空格(在Javascript中)?

I'm trying to work out what regular expression I would need to take a value and replace spaces with a dash (in Javascript)?

所以说如果我有北美,它会让我回到北美吗?

So say if I had North America, it would return me North-America ?

我可以做一些像 var foo = bar.replace('',' - ')

推荐答案

最好使用:

var string = "find this and find that".replace(/find/g, "found");

替换所有出现次数。

这篇关于正则表达式用短划线替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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