简体   繁体   English

Javascript字符串变量没有引用?

[英]Javascript string variable unquoted?

I am using the QuickBlox JavaScript API. 我正在使用QuickBlox JavaScript API。 Looking through their code, I found this line: 仔细查看他们的代码,我找到了这一行:

var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;

It appears that it has declared a string variable that is a regular expression pattern. 它似乎已经声明了一个正则表达式模式的字符串变量。 Then it goes ahead to use that variable thus: 然后继续使用该变量:

return str.replace(URL_REGEXP, function(match) {
        url = (/^[a-z]+:/i).test(match) ? match : 'http://' + match;
        url_text = match;
        return '<a href="' + escapeHTML(url) + '" target="_blank">' + escapeHTML(url_text) + '</a>';
    });

I am wondering how is this possible? 我想知道这怎么可能? The var declared in the first line should be a string, but it is unquoted. 在第一行中声明的var应该是一个字符串,但它是不带引号的。 Shouldn't this be a syntax error? 这不应该是语法错误吗?

I went ahead and tested this code on my browser, and it works! 我继续在我的浏览器上测试了这段代码,它确实有效! This mean's I've got some learning to do here... Can anyone explain how this variable is declared? 这意味着我在这里学到了一些东西......任何人都可以解释这个变量的声明方式吗?

Additionally, I tried to run the same code on my friends computer, the Chrome debugger throws a syntax error on the variable declaration line (unexpected token '/'). 另外,我尝试在朋友计算机上运行相同的代码,Chrome调试器会在变量声明行(意外标记'/')上引发语法错误。 I am using Chrome Version 36.0.1985.143 m, my friend is using the same thing, but on my computer, it all works fine, on my friends computer, the code stops at the first variable declaration because of "syntax error". 我使用的是Chrome版本36.0.1985.143 m,我的朋友正在使用同样的东西,但在我的电脑上,一切正常,在我的朋友计算机上,由于“语法错误”,代码在第一个变量声明处停止。

Is there some setting that is different? 是否有一些不同的设置?

Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE UPDATE

Thanks for the quick answers. 谢谢你的快速解答。 I've come from a PHP background, so thought that all regular expressions has to be initialized as strings :P. 我来自PHP背景,所以认为所有正则表达式都必须初始化为字符串:P。

Anyone can reproduce the syntax error I'm getting on my friends computer? 任何人都可以重现我在朋友计算机上遇到的语法错误? (It still happens after disabling all extensions). (它仍然在禁用所有扩展后发生)。 I can't reproduce it either, and that's what is frustrating me. 我也无法重现它,这就是令我沮丧的事情。

UPDATE 2 更新2

I have tested and my friends computer and looked through the source. 我已经测试过我和朋友的电脑,并查看了源代码。 It appear to be due to some encoding problems (I'm not sure what). 它似乎是由于一些编码问题(我不知道是什么)。 The regular expression is shown like this: 正则表达式如下所示:

var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?芦禄鈥溾€濃€樷€橾))/gi;

(The characters at the end of the code is some random chinese characters, it seems). (代码末尾的字符是一些随机的汉字,似乎)。

How can I change the encoding to match his browser/system? 如何更改编码以匹配其浏览器/系统? (He is running on a Windows 7 Chinese simplified system). (他正在运行Windows 7中文简化系统)。

It is not a String variable. 它不是String变量。 It is a regular expression . 这是一个正则表达式

Calling var varname = /pattern/flags; 调用var varname = /pattern/flags;
is effective to calling var varname = new RegExp("pattern", "flags"); 调用var varname = new RegExp("pattern", "flags");是有效的var varname = new RegExp("pattern", "flags"); .

You can execute the following in any browser that supports a JavaScript console: 您可以在任何支持JavaScript控制台的浏览器中执行以下操作:

>>> var regex = /(?:[\w-]+\.)+[\w-]+/i
>>> regex.exec("google.com")
... ["google.com"]
>>> regex.exec("www.google.com")
... ["www.google.com"]
>>> regex.exec("ftp://ftp.google.com")
... ["ftp.google.com"]
>>> regex.exec("http://www.google.com")

Anyone can reproduce the syntax error I'm getting on my friends computer? 任何人都可以重现我在朋友计算机上遇到的语法错误? (It still happens after disabling all extensions). (它仍然在禁用所有扩展后发生)。 I can't reproduce it either, and that's what is frustrating me. 我也无法重现它,这就是令我沮丧的事情。

According to RegExp - JavaScript documentation : 根据RegExp - JavaScript文档

Regex literals was present in ECMAScript 1st Edition, implemented in JavaScript 1.1. 正则表达式文字存在于ECMAScript第1版中,在JavaScript 1.1中实现。 Use an updated browser. 使用更新的浏览器。

No, it shouldn't be a syntax error. 不,它不应该是语法错误。 In Javascript, RegExp objects are not strings, they are a distinct class of objects. 在Javascript中, RegExp对象不是字符串,它们是不同的对象类。 /.../modifiers is the syntax for a RegExp literal. /.../modifiersRegExp文字的语法。

I can't explain the syntax error you got on your friend's computer, it looks fine to me. 我无法解释你在朋友的计算机上遇到的语法错误,这对我来说很好。 I pasted it into the Javascript console and it was fine. 我把它粘贴到Javascript控制台,很好。

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

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