IT業界のすみっこ暮らし

ふと気がついたときの記録



文字列のスクリプトタグ削除(Javascript)

タグのみ削除、空白は削除しない

function TagDelete(Text) {

    if (Text == "" || Text == null)
        return "";

    //スクリプト文字
    Text = Text.replace(/</g, "&lt;");//"<"をコードに変換(タグ無効化)
    Text = Text.replace(/>/g, "&gt;"); //">"をコードに変換(タグ無効化)
    Text = Text.replace(/(<([^>]+)>)/ig, "");
    
    return Text;
}

タグ削除、空白削除

function TagAndBlankDelete(Text) {

    if (Text == "" || Text == null)
        return "";

    //スクリプト文字
    Text = Text.replace(/(<([^>]+)>)/ig, "");
    Text = Text.replace(/\s+/g, "");
    
    return Text;
}




プライバシーポリシー