How to replace character in HTML string with javascript - Stack Overflow

I'm using sanitize-html to clean pasted text for draftJS editor.Lets say result might be text stri

I'm using sanitize-html to clean pasted text for draftJS editor.

Lets say result might be text string like this

<h1 class="title"> President said "<b>Give this man a money</b>" and i agree </h1

Now i need to replace " with « or » depends on conditions. How should i do that. I tried to figure out if i can do it with draftJS ContentBlock methods, but it seems way too plicated. So i think it is easier to modify html string.

I'm using sanitize-html to clean pasted text for draftJS editor.

Lets say result might be text string like this

<h1 class="title"> President said "<b>Give this man a money</b>" and i agree </h1

Now i need to replace " with « or » depends on conditions. How should i do that. I tried to figure out if i can do it with draftJS ContentBlock methods, but it seems way too plicated. So i think it is easier to modify html string.

Share Improve this question asked May 26, 2017 at 8:26 LauriLauri 651 gold badge4 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You can do this with two regular expressions I guess:

    var inputString = `<h1 class="title">
      President said "<b>Give this man a money</b>" and i agree
    </h1>`
      , startingQuoteRE = / "/g
      , endingQuoteRE = /" /g
      , outputString = ''
      ;
    outputString = inputString.replace(startingQuoteRE, " «");
    outputString = outputString.replace(endingQuoteRE, "» ");
    // Or by chaining .replace
    // outputString = inputString.replace(startingQuoteRE, " «").replace(endingQuoteRE, "» ");
    console.log(outputString);

  1. Create a replaceAll function. Because replace function will replace the only first occurrence.

    String.prototype.replaceAll = function(string, replace) {
    return this.split(string).join(replace);
    };
    
  2. Call the function like this.

    var str = '<h1 class="title">\
    President said "<b>Give this man a money</b>" and i agree\
    </h1>';
    var result = str.replaceAll('"','\'');
    console.log(result);
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745064835a4609192.html

相关推荐

  • How to replace character in HTML string with javascript - Stack Overflow

    I'm using sanitize-html to clean pasted text for draftJS editor.Lets say result might be text stri

    19小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信