Home > Mobile >  Remove/ Replace span of Specific String from the sentence
Remove/ Replace span of Specific String from the sentence

Time:03-25

I am trying to remove the span tag from string of sentence .Is it possible .?

My Input is as below :

divData = Welcome Hello <span> World </Span> ! Looking forward the great <span>things</span>.

Output I am looking for :

divData = Welcome Hello World ! Looking forward the great <span>things</span>.

what I have tried is :

  str = divData.replace(/<\/?span[^>]*>/g,"");

But above one is replacing all the span of all sentence.

CodePudding user response:

Try this,

str = divData.replace(/<span[^>]*>/,"");
str = str.replace(/<\/?Span[^>]*>/,"");

(Removed 'g' flag) 'g' means global, it will replace everything in the string.

CodePudding user response:

You may use replace here in non global mode, which will target the first span tag only:

divData = "Welcome Hello <span> World </span> ! Looking forward the great <span>things</span>.";
divData = divData.replace(/<span>\s*(.*?)\s*<\/span>/, "$1");
console.log(divData);

  • Related