开发者

How to replace text in a string without a selection

How to take something like

var value = 'Hello there';

and do a jQuery replace, like

$(value).replace('there','world');

Every example I can find will 开发者_StackOverflowshow you how to replace text that is in an HTML object, what if it's just in JS?

    • -

Is there a way to do this with jquery?

use the $ selector to select a variable?


value = value.replace('there','world');

If you want to replace every occurrence, you must use a regex with the g flag (even if it's just a normal string you want to replace):

value = value.replace( /there/g, 'world' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜