简体   繁体   English

比较两个具有不同编码的字符串

[英]Compare two Strings with different encoding

I'm writing a Greasemonkey script. 我正在写一个Greasemonkey脚本。 I need to compare two strings, where one of these is equal to document.location.href . 我需要比较两个字符串,其中之一等于document.location.href

If document.location.href is equal to "http://lema.rae.es/drae/?val=ñáñara" then I need to do something extra, but I can't determine if the two strings are equal, because document.location.href is converted to another character set. 如果document.location.href等于"http://lema.rae.es/drae/?val=ñáñara"那么我需要做些额外的事情,但是我无法确定两个字符串是否相等,因为document.location.href转换为另一个字符集。 This is an example: 这是一个例子:

var currentLocation = document.location.href.toString();
var targetLocation = 'http://lema.rae.es/drae/?val=ñáñara';

alert(currentLocation + '\n' + targetLocation);

/* OUTPUT:

    http://lema.rae.es/drae/?val=%C3%B1%C3%A1%C3%B1ara
    http://lema.rae.es/drae/?val=ñáñara
*/

How can I convert two strings to the same character set? 如何将两个字符串转换为相同的字符集?

Just use: 只需使用:

var url = decodeURI(window.location.href);

Should be enough. 应该足够了。

See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/decodeURI 请参阅: https//developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/decodeURI

encodeURI("http://lema.rae.es/drae/?val=ñáñara")encodeURI("http://lema.rae.es/drae/?val=ñáñara")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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