开发者

How to replace text in a string without a selection

开发者 https://www.devze.com 2023-02-05 14:51 出处:网络
How to take something like var value = \'Hello there\'; and do a jQuery replace, like $(value).replace(\'there\',\'world\');

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

精彩评论

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