Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

javascript - regexp resulting different matches when run from js file and chrome console

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...