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.

goku
  • 223
  • 2
  • 12
  • `str=str.replace(/

    ([\w\W]+?)<\/p>/g,'$1
    ');`

    – dandavis Jul 07 '15 at 04: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. – Amadan Jul 07 '15 at 05:04
  • possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –  Jul 07 '15 at 05:20

2 Answers2

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

Community
  • 1
  • 1
Harpreet Singh
  • 2,651
  • 21
  • 31
  • Thanks @Harpreet Singh it help me a lot ...And i also want to replace the corresponding closing with
    .How to do so.
    – goku Jul 07 '15 at 05:59
  • 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 Jul 07 '15 at 06:11
  • @goku, I think you have given some wrong input and the while loop is becoming infinite loop – Harpreet Singh Jul 07 '15 at 06:13
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>");
AkshayJ
  • 771
  • 6
  • 15