인코딩 대신 이스케이프를 사용해야 하는 경우URI / 인코딩요구성분?
때 - 합니까?escape()
요?encodeURI()
★★★★★★★★★★★★★★★★★」encodeURIComponent()
:
이스케이프 사용:
escape("% +&=");
또는
encodeURI() / encode 사용URIC 구성 요소()
encodeURI("http://www.google.com?var1=value1&var2=value2");
encodeURIComponent("var1=value1&var2=value2");
이스케이프()
★★★★★★★★★★★★★★★★!escape()
는 섹션 B.2.1.2 이스케이프에 정의되어 있으며 Annex B의 개요 텍스트에는 다음과 같이 기재되어 있습니다.
되어 있는 모든 및 않은 , 이 ..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................
ECMAScript 코드를 할 때 이나 동작의 는 안 프로그래머는 새로운 ECMAScript 코드를 작성할 때 이러한 기능이나 동작의 존재를 사용하거나 가정해서는 안 됩니다.
동작:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape
특수문자는 @*_+-/를 제외하고 부호화됩니다.
은 두 입니다.「 」 、 「 0xFF 」 、 「 16 」 、 「 」%xx
.
유닛이 큰 4자리 " " " " " " " " " " " " " 4 " " "%uxxxx
사용됩니다.이는 쿼리 문자열 내에서 허용되지 않습니다(RFC3986에서 정의).
query = *( pchar / "/" / "?" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded = "%" HEXDIG HEXDIG
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
는 두 개의 뒤에 16자리 가 이어지는 경우에만 됩니다.u
사용할 수 없습니다.
encodeURI()
인코딩 사용동작하는 URL 의 경우는, URI 를 실행합니다.이 호출은 다음과 같습니다.
encodeURI("http://www.example.org/a file with spaces.html")
입수 방법:
http://www.example.org/a%20file%20with%20spaces.html
부호화를 호출하지 않음URL이 파기되어 반환되기 때문에 URIC 구성 요소는
http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html
부호화에 주의해 주세요.인코딩과 같은 URIURIC 컴포넌트는 ' 문자를 벗어나지 않습니다.
encodeURIComponent()
인코딩 사용URL 파라미터 값을 인코딩하는 경우 URIC 컴포넌트.
var p1 = encodeURIComponent("http://example.org/?a=12&b=55")
그런 다음 필요한 URL을 만들 수 있습니다.
var url = "http://example.net/?param1=" + p1 + "¶m2=99";
완전한 URL이 표시됩니다.
http://example.net/?param1=http%3A%2F%2Fexample.org%2F%Ffa%3D12%26b%3D55¶m2=99
「 」는 「 encode 」라고 것에 주의해 .URIC 는 URIC를 벗어나지 .'
인 는 html Atribut 등의 html Atribut Atribute를 작성하기 사용합니다.href='MyUrl'
주입 버그가 발생할 수 있습니다. html을 합니다."
'
따옴표 부호화(「 따옴표」)'
%27)을(를) 사용하다
이러한 유형의 인코딩에 대한 자세한 내용은 http://en.wikipedia.org/wiki/Percent-encoding 를 참조하십시오.
「 」의 encodeURI()
★★★★★★★★★★★★★★★★★」encodeURIComponent()
11로 입니다.URIC를 사용하다 URI:
Google Chrome의 console.table을 사용하여 다음 코드를 사용하여 이 테이블을 쉽게 생성했습니다.
var arr = [];
for(var i=0;i<256;i++) {
var char=String.fromCharCode(i);
if(encodeURI(char)!==encodeURIComponent(char)) {
arr.push({
character:char,
encodeURI:encodeURI(char),
encodeURIComponent:encodeURIComponent(char)
});
}
}
console.table(arr);
나는 이 기사가 계몽적이라는 것을 알았다: Javascript Madness: 쿼리 문자열 해석
내가 왜 해독하는지 알아내려다 발견했어URIC 구성 요소가 '+'을(를) 올바르게 디코딩하지 않았습니다.다음은 발췌입니다.
String: "A + B"
Expected Query String Encoding: "A+%2B+B"
escape("A + B") = "A%20+%20B" Wrong!
encodeURI("A + B") = "A%20+%20B" Wrong!
encodeURIComponent("A + B") = "A%20%2B%20B" Acceptable, but strange
Encoded String: "A+%2B+B"
Expected Decoding: "A + B"
unescape("A+%2B+B") = "A+++B" Wrong!
decodeURI("A+%2B+B") = "A+++B" Wrong!
decodeURIComponent("A+%2B+B") = "A+++B" Wrong!
하지 않음URIC u u u u u u-_.!~*'()
이로 인해 xml 문자열의 php에 데이터를 게시하는 데 문제가 발생했습니다.
예를 들어 다음과 같습니다.
<xml><text x="100" y="150" value="It's a value with single quote" /> </xml>
(「」를 사용한)encodeURI
%3Cxml%3E%3Ctext%20x=%22100%22%20y=%22150%22%20value=%22It's%20a%20value%20with%20single%20quote%22%20/%3E%20%3C/xml%3E
보시다시피 단일 따옴표는 부호화되지 않았습니다.문제를 해결하기 위해 프로젝트의 문제를 해결하기 위해 Encoding URL에 대해 다음 두 가지 함수를 만들었습니다.
function encodeData(s:String):String{
return encodeURIComponent(s).replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
}
디코딩 URL의 경우:
function decodeData(s:String):String{
try{
return decodeURIComponent(s.replace(/\%2D/g, "-").replace(/\%5F/g, "_").replace(/\%2E/g, ".").replace(/\%21/g, "!").replace(/\%7E/g, "~").replace(/\%2A/g, "*").replace(/\%27/g, "'").replace(/\%28/g, "(").replace(/\%29/g, ")"));
}catch (e:Error) {
}
return "";
}
encodeURI() - excape() 함수는 javascript excape용이며 HTTP가 아닙니다.
작은 비교표 Java와JavaScript vs. PHP.
1. Java URLEncoder.encode (using UTF8 charset)
2. JavaScript encodeURIComponent
3. JavaScript escape
4. PHP urlencode
5. PHP rawurlencode
char JAVA JavaScript --PHP---
[ ] + %20 %20 + %20
[!] %21 ! %21 %21 %21
[*] * * * %2A %2A
['] %27 ' %27 %27 %27
[(] %28 ( %28 %28 %28
[)] %29 ) %29 %29 %29
[;] %3B %3B %3B %3B %3B
[:] %3A %3A %3A %3A %3A
[@] %40 %40 @ %40 %40
[&] %26 %26 %26 %26 %26
[=] %3D %3D %3D %3D %3D
[+] %2B %2B + %2B %2B
[$] %24 %24 %24 %24 %24
[,] %2C %2C %2C %2C %2C
[/] %2F %2F / %2F %2F
[?] %3F %3F %3F %3F %3F
[#] %23 %23 %23 %23 %23
[[] %5B %5B %5B %5B %5B
[]] %5D %5D %5D %5D %5D
----------------------------------------
[~] %7E ~ %7E %7E ~
[-] - - - - -
[_] _ _ _ _ _
[%] %25 %25 %25 %25 %25
[\] %5C %5C %5C %5C %5C
----------------------------------------
char -JAVA- --JavaScript-- -----PHP------
[ä] %C3%A4 %C3%A4 %E4 %C3%A4 %C3%A4
[ф] %D1%84 %D1%84 %u0444 %D1%84 %D1%84
저는 그 방법 중 하나를 그대로 사용하지 않는 것을 추천합니다.옳은 일을 하는 당신만의 기능을 쓰세요.
MDN은 다음과 같은 URL 인코딩의 좋은 예를 제시했습니다.
var fileName = 'my file(2).txt';
var header = "Content-Disposition: attachment; filename*=UTF-8''" + encodeRFC5987ValueChars(fileName);
console.log(header);
// logs "Content-Disposition: attachment; filename*=UTF-8''my%20file%282%29.txt"
function encodeRFC5987ValueChars (str) {
return encodeURIComponent(str).
// Note that although RFC3986 reserves "!", RFC5987 does not,
// so we do not need to escape it
replace(/['()]/g, escape). // i.e., %27 %28 %29
replace(/\*/g, '%2A').
// The following are not required for percent-encoding per RFC5987,
// so we can allow for a little better readability over the wire: |`^
replace(/%(?:7C|60|5E)/g, unescape);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
javascript 인코딩을 위해 세 가지 함수가 내장되어 있습니다.
escape()
안 함 - " "@*/+
이 방법은 ECMA 3 이후에는 권장되지 않으므로 피해야 합니다.encodeURI()
안 함 - " "~!@#$&*()=:/,;?+'
URI가 완전한 URI라고 가정하기 때문에 URI에 특별한 의미가 있는 예약된 문자는 인코딩되지 않습니다.은 특정 URL은 URL로 변환됩니다.encodeURI('http://stackoverflow.com');
제공 - http://stackoverflow.comencodeURIComponent()
안 함 - " "- _ . ! ~ * ' ( )
이 함수는 특정 문자의 각 인스턴스를 UTF-8 인코딩을 나타내는1개, 2개, 3개 또는4개의 이스케이프 시퀀스로 대체함으로써 Uniform Resource Identifier(URI; 유니폼자원 식별자) 컴포넌트를 인코딩합니다.를 들어 일부 에는 URL을 예를 들어 일부 사용자 입력을 추가해야 합니다. 예 -encodeURIComponent('http://stackoverflow.com');
3give - http%3을합니다.%2F%2Fstackoverflow.com
이 모든 인코딩은 UTF 8로 실행됩니다.즉, 문자는 UTF-8 형식으로 변환됩니다.
encodeURIC 컴포넌트가 encode와 다르다예약된 문자와 인코딩 번호 기호 번호를 인코딩하는 URIURI
또, 모두 다른 문자 세트를 인코딩 해, 필요한 문자를 적절히 선택합니다.encodeURI()는 인코딩보다 적은 문자를 인코딩합니다.URIComponent(): excape()보다 적은 수의 문자(또 다른 문자도 dannyp의 포인트에 부호화).
한번 해보세요.encodeURI()
★★★★★★★★★★★★★★★★★」encodeURIComponent()
★★★★★★★★★★★★...
console.log(encodeURIComponent('@#$%^&*'));
력::@#$%^&*
: . 력 :%40%23%24%25%5E%26*
래, 깐, 이, 이, 이, . 、 . 、 . 、 . 、 . 、 . 、 . 、 . 、 . 、 。*
왜게 변환 ?환 ??? 경우 반드시 할 수 .linux command "$string"
- 정말 원하는 게... - 긴 이야기...
언제 사용합니까?절대.encodeURI()
는 브래킷 인코딩에 관해 RFC3986을 준수하지 않습니다.fixedEncodeURI()
MDN encodeURI() " "에서 정의 및 자세한 설명을 하고 있습니다.
function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }
언제 사용합니까?절대.encodeURIComponent()
인코딩에 .RFC3986은 RFC3986을 준수하지 않습니다.!'()*
을 사용하다fixedEncodeURIComponent()
MDN 인코딩에서 정의 및 자세한 설명에 따라URIComponent() 문서...
function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }
ㄴ, ㄴ, ㄴ, ㄴ, ㄴ데.fixedEncodeURI()
"URL"은 "URL", "URL", "URL"은 "URL"은 "URLfixedEncodeURIComponent()
및단순히 을 인코딩합니다.★★★★★★★★★★★★★★★★★★★★★★★·fixedEncodeURI()
는 부호화되지 .+@?=:#;,$&
: (예:)&
★★★★★★★★★★★★★★★★★」+
연산자)입니다만, 「URL」은 「URL」입니다.fixedEncodeURIComponent()
contracty underbanda 。
여러 가지 방법을 실험해 보면 다양한 용도와 기능을 잘 파악한 후에도 건전성을 확인할 수 있다는 것을 알 수 있습니다.
그 때문에, 이 Web 사이트는, 내가 적절한 행동을 하고 있는 것이 아닌가 하는 의심을 확인하는 데 매우 도움이 되었습니다.또한 인코딩 복호화에도 유용하다는 것이 증명되었습니다.해석하기 어려운 URL 구성 요소 문자열입니다.최적의 북마크:
http://www.the-art-of-web.com/javascript/escape/
인정된 답변은 좋습니다.마지막 부분을 확장하려면:
부호화에 주의해 주세요.URIC 구성 요소는 ' 문자를 벗어나지 않습니다.일반적인 버그는 href='MyUrl'과 같은 html 속성을 생성하기 위해 이 버그를 사용하는 것입니다.문자열에서 html을 작성하는 경우 속성 따옴표로 " 대신 "을 사용하거나 인코딩 레이어(%27로 인코딩할 수 있음)를 추가합니다.
안전한 편에 서려면 예약되지 않은 문자를 백분율로 인코딩해야 합니다.
이 방법을 사용하여 이스케이프할 수 있습니다(소스 Mozilla).
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
// fixedEncodeURIComponent("'") --> "%27"
요한의 책상에 영감을 받아 책상을 확장하기로 했어요어떤 ASCII 문자가 인코딩되는지 보고 싶었어요.
var ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
var encoded = [];
ascii.split("").forEach(function (char) {
var obj = { char };
if (char != encodeURI(char))
obj.encodeURI = encodeURI(char);
if (char != encodeURIComponent(char))
obj.encodeURIComponent = encodeURIComponent(char);
if (obj.encodeURI || obj.encodeURIComponent)
encoded.push(obj);
});
console.table(encoded);
표에서는 부호화된 문자만 보여 줍니다.빈 셀은 원래 문자와 인코딩된 문자가 동일함을 의미합니다.
덧붙여서 vs 테이블을 하나 더 추가하겠습니다.유일한 차이점은 공백 문자의 인코딩인 것 같습니다.
<script>
<?php
$ascii = str_split(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", 1);
$encoded = [];
foreach ($ascii as $char) {
$obj = ["char" => $char];
if ($char != urlencode($char))
$obj["urlencode"] = urlencode($char);
if ($char != rawurlencode($char))
$obj["rawurlencode"] = rawurlencode($char);
if (isset($obj["rawurlencode"]) || isset($obj["rawurlencode"]))
$encoded[] = $obj;
}
echo "var encoded = " . json_encode($encoded) . ";";
?>
console.table(encoded);
</script>
@johann-echavarria의 답변의 현대적 개서:
console.log(
Array(256)
.fill()
.map((ignore, i) => String.fromCharCode(i))
.filter(
(char) =>
encodeURI(char) !== encodeURIComponent(char)
? {
character: char,
encodeURI: encodeURI(char),
encodeURIComponent: encodeURIComponent(char)
}
: false
)
)
테이블을할 수 는, 「」를 치환해 .console.log
console.table
( ( ( ( ( ( ( ( ( ( ( 。
난 이런 기능이 있어
var escapeURIparam = function(url) {
if (encodeURIComponent) url = encodeURIComponent(url);
else if (encodeURI) url = encodeURI(url);
else url = escape(url);
url = url.replace(/\+/g, '%2B'); // Force the replacement of "+"
return url;
};
언급URL : https://stackoverflow.com/questions/75980/when-are-you-supposed-to-use-escape-instead-of-encodeuri-encodeuricomponent
'itsource' 카테고리의 다른 글
키별로 JavaScript 개체 정렬 (0) | 2022.10.26 |
---|---|
foreach 루프의 어레이 값 수정 (0) | 2022.10.25 |
jQuery에 요소가 있는지 확인합니다. (0) | 2022.10.25 |
대규모 프로젝트의 경우 스프링 데이터 JPA와 휴지 상태와의 차이점은 무엇입니까? (0) | 2022.10.25 |
Java에서 TimeStamp를 최신 상태로 변환하는 방법 (0) | 2022.10.25 |