简体   繁体   English

JavaScript非UTF-8字符搜索Google Chrome扩展程序

[英]Javascript Non UTF-8 Character Searching for Google Chrome Extension

Edit: 编辑:

I am creating a Chrome Extension and the files must be UTF-8 encoded. 我正在创建Chrome扩展程序,并且文件必须为UTF-8编码。 I use JQuery to get contents from page, and check that if that contains specific strings that contains Ö, ı and İ. 我使用JQuery从页面获取内容,并检查其中是否包含包含Ö,ı和İ的特定字符串。 However, because the Chrome forces files must be encoded UTF-8; 但是,由于Chrome浏览器强制文件必须编码为UTF-8,因此, I cannot perform a search of "İ, ı, Ö". 我无法搜索“İ,ı,Ö”。

var p = txt.indexOf("İ"); 

Does not work as I need because I cannot save the files with İ, Ö or ı. 无法按我的需要工作,因为我无法使用İ,Ö或ı保存文件。

JavaScript string literals include a syntax for expressing special characters . JavaScript字符串文字包括用于表达特殊字符语法

For instance, 'Ö' and '\Ö' are identical strings in JavaScript. 例如,“Ö”和“ \\ u00D6”在JavaScript中是相同的字符串。

To find the unicode literal for a specific character, you can do this: 要查找特定字符的unicode文字,您可以执行以下操作:

'Ö'.charCodeAt(0).toString(16); // yields "d6"; the code is "\u00D6"

Therefore, to search for a Ö in a string, you could do: 因此,要在字符串中搜索Ö,可以执行以下操作:

var toSearch = "abc Ö def";
if (toSearch.indexOf('\u00D6') > -1) {
    // found!
}

If you need further help, try posting a code sample. 如果您需要更多帮助,请尝试发布代码示例。

encodeURIComponent(your string)                               

encode it when you save the file, and encode the string before you search 保存文件时对其进行编码,然后在搜索之前对字符串进行编码

http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp

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

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