简体   繁体   English

用一行 Javascript 替换不区分大小写的单词

[英]Replace case insensitive word with a line Javascript

    var fs = require('fs')
fs.readFile(someFile, 'utf8', function (err,data) {

  var formatted = data.replace(/problem(.*)/gm, 'hiding this word');

 fs.writeFile(someFile, formatted, 'utf8', function (err) {
    if (err) return console.log(err);
 });
});

for example if there is uppercase word like PROBLEM find the someFile.It should replace it with "hiding this word".例如,如果有像 PROBLEM 这样的大写单词,请找到 someFile。它应该用“隐藏这个单词”替换它。 Can anyone please help?有人可以帮忙吗?

Just use the i flag: /problem(.*)/gmi只需使用i标志:/ /problem(.*)/gmi

 let data = "Here is the pROblem"; let formatted = data.replace(/problem(.*)/gmi, 'hiding this word'); console.log(formatted)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM