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' );
精彩评论