当从js文件和chrome控制台运行时,regexp产生不同的匹配 [英] regexp resulting different matches when run from js file and chrome console

查看:102
本文介绍了当从js文件和chrome控制台运行时,regexp产生不同的匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  alert(您好@name,您的账户将会是在@date激活.match(/ @ {1} [\w _] + / g)); 

当我从文件运行代码时(使用浏览器打开文件)
我得到

  [你好,名字,你的,帐户,被激活,日期] 

但是,当我在 chrome开发者控制台

上运行它时get:

  [@ name,@ date] 

这里发生了什么?相同的代码d / t结果

更新:

@ funkwurm 试图在评论中提及使用Chrome开发人员工具检查页面时对代码进行的更改。

match(/ @ { 1} [\w _] + / g)改为 match(/ \ [\w _] + / g) p>

我使用 Grails 框架,所以我认为使用 slashy语法(/ /)导致了问题,因为Grails也使用该语法(由于冲突)。



因此将其更改为

  var pattern = new RegExp(@ {1} [\\w _] +,g); 


alert(你好@名,你的账号将在@date时激活.match(pattern));

但仍然没有效果,当我做 alert(pattern) code>我得到 / \ [\w _] + / g 仍然没有@。



更新 em>问题是,在使用Chrome开发人员工具检查它时缺少@字符(谢谢 @Lee Kowalkowski 了解详情)。



我试过 转义 ,但仍无法使用。



以下是我提出的解决方案:

  var pattern = new RegExp(String.fromCharCode(64)+{1} [\\w _] +,g); // 64是@ 

的ascii值仍然渴望知道为什么@字符丢失了


This is weird, I have this statement in js code:

alert ( "Hello @name , your account will be activated at  @date".match(/@{1}[\w_]+/g) );

When I run the code from the file (opening the file using browser) I get

[hello,name,your,account,be,activated,date]

But when I run it on the chrome developer console
I get:

[@name,@date]

what is happening here? same code d/t result

update:

As @funkwurm tried to mention on the comment there is a change on the code when I inspect the page using chrome developer tool.
match(/@{1}[\w_]+/g) was changed to match(/\[\w_]+/g)

I am using Grails framework so I thought using the slashy syntax (/ /) was causing the problem because Grails also uses that syntax ( due to conflict).

so changed it to

var pattern = new RegExp("@{1}[\\w_]+","g");    


alert ( "Hello @name , your account will be activated at @date".match(pattern) );

but still no effect, and when I do alert(pattern) I get /\[\w_]+/g still without the @.

Thanks

解决方案

As I tried to mention on the update the problem was the @ character was missing when inspecting it using chrome developer tool ( Thanks @Lee Kowalkowski for the insight).

I tried escaping it but still did not work.

Here is the solution I came up with:

var pattern = new RegExp(String.fromCharCode(64)+"{1}[\\w_]+","g"); //64 is ascii value of @

Still eager to know why @ character was missing

这篇关于当从js文件和chrome控制台运行时,regexp产生不同的匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆