简体   繁体   中英

Remove Specific Events From URL string using javascript

I have a URL

http://www.example.com/" onmouseover="alert('howdy')

In this URL I wanted to remove all events like onmouserover , onmouseclick , etc.

I have tried below code to extract just URL from String :

function UrlExtract(Url) {

var pattern = new RegExp(/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm);
Url = String(Url).match(pattern);
return Url;
}

What I want is user to specify which events to be removed from a Url string and only those events should get removed. Please help me with this.

Check the below code

var url = "example.com/; onmouseover=alert('howdy');onclick=alert('click'); onmouseout=alert('out')"
function removeString(originalStr, strArr) {
    if(strArr.constructor === Array) {
       for(var i = 0; i < strArr.length; i++) {
          originalStr = originalStr.replace(strArr[i], '');
       }
    }
    return oriStr;
}
console.log(removeString(url, [" onclick=alert('click');", " onmouseover=alert('howdy');"]));
//output: "example.com/; onmouseout=alert('out')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM