如何用另外多个单词搜索和替换多个单词?并且引号之间没有变化? [英] How to search and replace multiple words with onother multiple words ? and without change between quoted marks ?
问题描述
我想更改此字符串..
str =dw('download');
到这个字符串..
document.write('download');
不知怎的,它的回报..
d.write('dowritenload');
这里我不想改变''或'之间的字母标记并返回d作为文档。
i试图在w3schools找到它。有每个代码的样本,但我不知道如何在我的脚本中相互安排这些代码?
我尝试过: < br $> b $ b
i want to change this string..
str = "d.w('download');"
to this string..
document.write('download');
somehow its returns..
d.write('dowritenload');
here i dont want to change letters between '' or "" marks and returns d as document.
i tried to find it on w3schools. there are every code's sample but i dont know how to arrange those code with each other in my script ?
What I have tried:
推荐答案
str = "d.w('download');"
re = str.replace(/d/g, "document");
re = str.replace(/w/g, "write");
您可能希望将您的替换限制为'd'和'w'的第一个发生率。检查你的正则表达式语法并从那里弄清楚。
例如,回到你的w3Schools参考: RegExp g Modifier [ ^ ]
试试
try
str = "d.w('download');"
str = str.replace('d.w', "document.write");
// or
str = str.replace('d.', "document.");
str = str.replace('w(', "write(");
这篇关于如何用另外多个单词搜索和替换多个单词?并且引号之间没有变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!