Wrap certain word with using jquery

后端 未结 2 865
既然无缘
既然无缘 2021-01-13 00:36

I have the following div:

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 01:34

    Starting with a contenteditable element we can replace the markup as we need by operating directly on its innerHtml:

    $('#query-container').on('keyup', function(e){
      var $this = $(this);
      //(?!\<\/b\>) negative lookahead is used so that anything already wrapped
      //into a markup tag would not get wrapped again
      $this.html($this.html().replace(/(SELECT|UPDATE|DELETE)(?!\<\/b\>)/gi, '$1'));
      setEndOfContenteditable(this);
    });
    

    IMO this is a more readable option. Add the rangeselect method from the previous answer and we have a working fiddle

提交回复
热议问题