Last active
August 29, 2015 14:16
-
-
Save hrdaya/d041d27110f7dcab3a7a to your computer and use it in GitHub Desktop.
JavaScriptで正規表現(エスケープ編) ref: http://qiita.com/hrdaya/items/4beebbdb57009b405d2d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HTMLエスケープ | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var escapeHTML = function (str) { | |
return str | |
.replace(/&/g, '&') | |
.replace(/</g, '<') | |
.replace(/>/g, '>') | |
.replace(/"/g, '"') | |
.replace(/'/g, '''); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HTMLアンエスケープ | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var unEscapeHTML = function (str) { | |
return str | |
.replace(/(<)/g, '<') | |
.replace(/(>)/g, '>') | |
.replace(/(")/g, '"') | |
.replace(/(')/g, "'") | |
.replace(/(&)/g, '&'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JavaScriptエスケープ | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var escapeJs = function (str) { | |
return str | |
.replace(/\\/g, '\\\\') | |
.replace(/'/g, "\\'") | |
.replace(/"/g, '\\"') | |
.replace(/\//g, '\\/') | |
.replace(/</g, '\\x3c') | |
.replace(/>/g, '\\x3e') | |
.replace(/(0x0D)/g, '\r') | |
.replace(/(0x0A)/g, '\n'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JavaScriptアンエスケープ | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var unEscapeJs = function (str) { | |
return str | |
.replace(/\\'/g, "'") | |
.replace(/\\"/g, '"') | |
.replace(/\\\//g, '/') | |
.replace(/\\x3c/g, '<') | |
.replace(/\\x3e/g, '>') | |
.replace(/\\\\/g, '\\'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JavaScriptとHTMLのエスケープ | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var escapeJsHTML = function (str) { | |
return str | |
.replace(/\\/g, '\\\\') | |
.replace(/'/g, "\\'") | |
.replace(/"/g, '\\"') | |
.replace(/\//g, '\\/') | |
.replace(/</g, '\\x3c') | |
.replace(/>/g, '\\x3e') | |
.replace(/(0x0D)/g, '\r') | |
.replace(/(0x0A)/g, '\n') | |
.replace(/&/g, '&'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 外部リンク不可、括弧類のエスケープ | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
var noScript = function (str) { | |
return str | |
.replace(/(\/\/)/g, '//') | |
.replace(/\(/g, '(') | |
.replace(/\)/g, ')') | |
.replace(/\[/g, '[') | |
.replace(/\]/g, ']') | |
.replace(/\{/g, '{') | |
.replace(/\}/g, '}'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列strをUTF-8ベースでエンコード | |
* | |
* 特別な意味を持つ予約文字「/」「:」「&」「+」「=」はエンコードしない | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
encodeURI(str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* encodeURI、もしくはそれに類似したルーチンによって作成された | |
* URIの要素をデコード | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
decodeURI(str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 文字列strをURIを構成する部分文字列としてエンコード | |
* | |
* URIで使用する記号をすべてエンコードするため、完全なエンコードになります。 | |
* 特別な意味を持つ予約文字「/」「:」「&」「+」「=」などもエンコードする為 | |
* URI全体に適用するとそれ自体はURIとして機能しなくなる。 | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
encodeURIComponent(str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* encodeURIComponent、もしくはそれに類似したルーチンによって作成された | |
* URIの要素をデコード | |
* | |
* @param {String} str 変換したい文字列 | |
*/ | |
decodeURIComponent(str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment