如何将单词放在引号之间,然后用从文件中读取的其他内容替换它
注意:我知道如何从文件中读写,我只需要知道如何从内引号中获取单词
如果您知道要查找的单词:
let text = 'Sentence with "word" in it';
let wordFromFile = "nothing";
let newText = text.replace('"word"', wordFromFile);
否则正则表达式是有用的。
使用Regex(正则表达式(和Regex替换:
let str = 'How can I get the word in between "quotation marks"';
let word = str.replace(/.*"(.*?)".*/g,'$1');
let replacedWord = 'single quotes';
console.log(str.replace(word,replacedWord))
您可以找到带引号的单词,例如word
by/"[^"]+"/
,并将其替换为其他单词,如key
const str = 'This is the "word" which inside two quotation marks'
const result = str.replace(/"[^"]+"/g, 'key')
console.log(result)