$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Javascript case-insensitive matching and replacing? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript case-insensitive matching and replacing? - Stack Overflow

matteradmin10PV0评论

Basically, I need to be able to find certain words (by 'word' I mean a set of characters) in a string (case insensitive) and if they match, I need to insert a symbol after the first letter of that particular set of characters. I can't use search replace, as that would not preserve the case.

Example:

Brown brownies are in an oven.

If the word I'm looking for is brown, and the character I want to insert is *, the result should be:

B*rown b*rownies are in an oven.

What is the best way to do so in JS?

Basically, I need to be able to find certain words (by 'word' I mean a set of characters) in a string (case insensitive) and if they match, I need to insert a symbol after the first letter of that particular set of characters. I can't use search replace, as that would not preserve the case.

Example:

Brown brownies are in an oven.

If the word I'm looking for is brown, and the character I want to insert is *, the result should be:

B*rown b*rownies are in an oven.

What is the best way to do so in JS?

Share Improve this question asked Aug 8, 2011 at 22:37 krbkrb 16.4k29 gold badges114 silver badges186 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

Regex with option 'ig' does the trick.

"Brown brownies are in an oven.".replace(/(b)(rown)/gi, "$1*$2")
function astAfterFirstLetter(words) {
  var re = new RegExp("\\b(?=" + words.join("|") + "\\b)(\\w)(\\w*)", "gi");
  return function (str) { return str.replace(re, "$1*$2"); };
}

astAfterFirstLetter(["brown", "cow"])("How now brown cow!")

produces

How now b*rown c*ow!

You can use regex, something like:

var re = /(B)(rown)/gi;

console.log("Brown brownies are in an oven".replace(re, "$1*$2"));
var str = 'Brown brownies are in an oven.'
var s = 'brown';
var r = '*';
var re = new RegExp('('+s.substr(0,1)+')('+s.substr(1)+')','ig');
log(str.replace(re, '$1'+r+'$2'));

But you will need to watch s for the characters that have some special meaning to regular expressions (https://developer.mozilla/en/JavaScript/Reference/Global_Objects/regexp) and will need to take care setting r too. Will fail also if s's length is less than 2.

Post a comment

comment list (0)

  1. No comments so far