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.)