jQuery-替换字符串中字符的所有实例 [英] jQuery - replace all instances of a character in a string

查看:298
本文介绍了jQuery-替换字符串中字符的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行不通,我非常需要

$('some+multi+word+string').replace('+', ' ' );

总是得到

some multi+word+string

它总是只替换第一个实例,但是我需要它可以用于所有+符号.

it's always replacing for the first instance only, but I need it to work for all + symbols.

推荐答案

您需要使用正则表达式,以便可以指定全局(g)标志:

You need to use a regular expression, so that you can specify the global (g) flag:

var s = 'some+multi+word+string'.replace(/\+/g, ' ');

(我删除了字符串周围的$(),因为replace不是jQuery方法,所以根本无法使用.)

(I removed the $() around the string, as replace is not a jQuery method, so that won't work at all.)

这篇关于jQuery-替换字符串中字符的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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