Bubba Yakoza
Bubba Yakoza

Reputation: 799

How do I strip <link> tag from javascript String object using Regex or some better alternative?

Here is code:

   <div class="rightColoumn" id="divreports">
       <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon">
       <link href="../ClientScript/General.css" rel="stylesheet" type="text/css">
    </div>

I wish I could remove lines completely.

javascript String object strObject contains this html.

Upvotes: 0

Views: 773

Answers (4)

channasmcs
channasmcs

Reputation: 1156

Here try this way to remove html tag

    //link
      var reg= /<link\b[^>]*?>/gi



    return this

        .replace(reg, '$1<link href="http://$2">$2>')

};

Upvotes: 0

Abdul Jabbar
Abdul Jabbar

Reputation: 2573

here's another way to remove < link tag lines:

strObject.replace(/^.*<link.*$/mg, "");

It will work if your string has line break '\n' at the end of each line.

Upvotes: 0

adeneo
adeneo

Reputation: 318182

How about just parsing it as what it is, HTML

var strObject = '<div class="rightColoumn" id="divreports"><link href="/favicon.ico" rel="shortcut icon" type="image/x-icon"><link href="../ClientScript/General.css" rel="stylesheet" type="text/css"></div>';

var parser = new DOMParser();
var doc    = parser.parseFromString(strObject, "text/html");
var parent = doc.getElementById('divreports');
var links  = parent.getElementsByTagName('link');

for (var i=links.length; i--;) parent.removeChild(links[i]);

FIDDLE

Upvotes: 2

Tim.Tang
Tim.Tang

Reputation: 3188

remove all <link> tag?

   var reg= /<link\b[^>]*?>/gi

Regular expression visualization

Debuggex Demo

Upvotes: 2

Related Questions