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.
2 Answers
Reset to default 2You 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);
Create a
replaceAll
function. Becausereplace
function will replace the only first occurrence.String.prototype.replaceAll = function(string, replace) { return this.split(string).join(replace); };
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
评论列表(0条)