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

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

问题描述

这不起作用,我非常需要它

This does not work and I need it badly

$('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天全站免登陆