0

Hi I am having the string which contains html content and I want use javascript to replace the tag <p class="MsoNormal"> with '' empty space and i want to replace corresponding closing tag </p> with <br> tag in that string.

If I use

first line:

str=str.replace(/<p class=\"MsoNormal\">/g,'');    

second line: str=str.replace(/<\/p>/g,'<br>');

All the closing </p> tag get remove .But i want to replace the closing </p> tag which has the opening tag of "<p class="MsoNormal">".

The first line of script is okay of me .What should i use to replace that corresponding closing tag in the second line.

3
  • str=str.replace(/<p class=\"MsoNormal\">([\w\W]+?)<\/p>/g,'$1<br>'); Commented Jul 7, 2015 at 4:56
  • Note that this solution is not general, as it does not support nesting. Luckily, paragraphs shouldn't be nested. But doing this with real DOM is much more universal. Commented Jul 7, 2015 at 5:04
  • possible duplicate of RegEx match open tags except XHTML self-contained tags Commented Jul 7, 2015 at 5:20

2 Answers 2

1

Check this: Output is what I got from your question is to replace with Empty String

var replaceTag = function(str, replaceTagString, endTagString) {
    var str = '';
    while(str.indexOf(replaceTagString) != -1) {
        //search for </p> after my matched String
        var indexOfClosingTag = str.indexOf(endTagString, str.indexOf(replaceTagString))
        //Replace </p> using Substring
        str = str.substr(0,indexOfClosingTag) + "<br>" + str.substr(indexOfClosingTag + endTagString.length,k.length)
        //Replace your main String
        str = str.replace(replaceTagString,'')
    }
    return str
}

var k = "<p class='MsoNormal'>something</p><p>other p tag</p><h1>I am h1</h1><p>Hello p</p><p class='MsoNormal'>Replace My Tag too</p>"

replaceTag(k, "<p class='MsoNormal'>", "</p>")

Output:

"something<p>other p tag</p><h1>I am h1</h1><p>Hello p</p>Replace My Tag too"

Concept:

string.indexOf(searchvalue,start)

Start searching for End of the Tag (P) after my current matched string position

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Harpreet Singh it help me a lot ...And i also want to replace the corresponding closing </p> with <br>.How to do so.
Thanks @Harpreet Singh I am getting error at line "var indexOfClosingTag = str.indexOf(endTagString, str.indexOf(replaceTagString))" --> A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete. And i stop the scripts.
@goku, I think you have given some wrong input and the while loop is becoming infinite loop
0

Define a function yourself like this-->

String.prototype.replaceAt=function(index, character) {
        return this.substr(0, index) + character + this.substr(index+character.length);
    }

And use it like this:

str = str.replaceAt(3, "</p>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.