Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

regex - How to check the string is empty after replace all <br/> and &nbsp; in javascript?

I'm trying to use regex to replace all <br /> tags and &nbsp; character from the data of ckeditor and validate if it's empty will alert an error. But somehow if i press enter multiple time in ckeditor, it will generate multiple line with content like this:

<br />
<br />
<br />
<br />
<br />
&nbsp;

I wrote this code and try to console out the result but it print out something like a blank string with break lines, not <empty string>. Is there any wrong in my code?

var str = "<br />
"+
"<br />
"+
"<br />
"+
"<br />
"+
"<br />
"+
"&nbsp;";

str = str.replace(/(&nbsp;)*/gm,'');
str = str.replace(/^s*$/gm,'');
str = str.replace(/<brs*/?>/gm, "");

console.log(str);
question from:https://stackoverflow.com/questions/65837846/how-to-check-the-string-is-empty-after-replace-all-br-and-nbsp-in-javascrip

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finnally, i found the resolution. I also have to remove in string, the code will run properly.

str = str.replace(/
/gm,'');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...