7
Javascript String.replace(): undocumented feature
Explorer
,
/t5/indesign-discussions/javascript-string-replace-undocumented-feature/td-p/10757152
Nov 22, 2019
Nov 22, 2019
Copy link to clipboard
Copied
ESTK object model viewer describes string.replace() function as following:
String.replace (what: any, with: string):
string Core JavaScript Classes
what: Data Type: any
with: Data Type: string
But as you probably know, this function doesn't replace all occurences in a string.
var s = 'old old old';
var ss = s.replace('old', 'new');
$.writeln(ss);
Result: new old old
Therefore I have always used a regular expression with 'g' flag as 1st argument to replace all occurences in a string:
var s = 'old old old';
var sss = s.replace(/old/g, 'new');
$.writeln(sss);
Result: new new new
But as I discovered recently, we can still use the default syntax if we use regular expression flag(s) as third argument:
var s = 'old old old';
var ssss = s.replace('old', 'new', 'g');
$.writeln(ssss);
Result: new new new
Maybe someone will find it useful 🙂
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Beginner
,
/t5/indesign-discussions/javascript-string-replace-undocumented-feature/m-p/14467714#M564695
Mar 05, 2024
Mar 05, 2024
Copy link to clipboard
Copied
very useful, thank you!
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
LATEST
/t5/indesign-discussions/javascript-string-replace-undocumented-feature/m-p/14467831#M564700
Mar 05, 2024
Mar 05, 2024
Copy link to clipboard
Copied
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

