Replace all instances of a pattern with regular expressions in Javascript / jQuery

前端 未结 2 1708
执笔经年
执笔经年 2021-01-20 00:33

First off I don\'t know much about regex and need to buy a book because it\'s has proven to me to be difficult to pickup.

Ultimately I want to take a dom element, an

2条回答
  •  既然无缘
    2021-01-20 01:02

    This will loop through an entire string and replace it with your chosen word or phrase with another. (kind of complicated but it works and is very reusable)

    var string = "this is some test text. You can replace all instances of any word/phrase within this text"

    var new string = string.findAndReplace("text", "BOO!");

    Object.prototype.findAndReplace = function( searchText, replace ) {
        var matchCount = 0;
            var text = this;
    
            for( var i = 0; i

提交回复
热议问题