如何用javascript替换HTML字符串中的字符

时间:2017-05-26 08:26:05

标签: javascript html draftjs

我正在使用#include <stdio.h> int main(void) { int a = 0; int b = 0; int correctInput=0; int total_sum = 0; do { printf("Type the first number : \n"); scanf("%d", &a); printf("Type the second number : \n"); scanf("%d", &b); if(a<b) correctInput=1; else printf("The second number should be bigger than the first one.\n"); } while (correctInput ==0) ; while (a <= b) { total_sum += a; a++; } printf("Result : %d \n" , total_sum); return 0; } 来清除sanitize-html编辑器的粘贴文本。

让我们说结果可能是像这样的文本字符串

draftJS

现在我需要将<h1 class="title"> President said "<b>Give this man a money</b>" and i agree </h1 替换为"«取决于条件。 我该怎么做我试图弄清楚我是否可以使用» draftJS方法来完成它,但它似乎太复杂了。所以我认为修改html字符串更容易。

2 个答案:

答案 0 :(得分:2)

我猜你可以用两个正则表达式来做到这一点:

&#13;
&#13;
    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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

  1. 创建replaceAll功能。因为replace函数将替换唯一的第一次出现。

    String.prototype.replaceAll = function(string, replace) {
    return this.split(string).join(replace);
    };
    
  2. 像这样调用函数。

    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);