开发者

SyntaxError: Unexpected token ILLEGAL caused by \u2028 and \u2029

开发者 https://www.devze.com 2023-04-11 10:22 出处:网络
I am sending a sting as a parameter to a JavaScript function: theJSFunction(\'say hi dude\'); Chrome gives me SyntaxError: Unexpected token ILLEGAL so after research, I\'ve 开发者_运维技巧found tha

I am sending a sting as a parameter to a JavaScript function:

theJSFunction('say hi dude');

Chrome gives me SyntaxError: Unexpected token ILLEGAL so after research, I've 开发者_运维技巧found that the following whitespaces generates the error \u2028 and \u2029.

The problem is, the string posted to the function is printed via PHP and I need it to be printed via PHP (could use ajax, but I am required to let the PHP print it).

Is there any way to remove those to characters through PHP or JavaScript?


You can replace with .replace. Passing a regular expressions with the g flag replaces all occurences, and you can include \uxxxx characters as well:

"\u2028\u2029".replace(/\u2028|\u2029/g, "").length; // 0
0

精彩评论

暂无评论...
验证码 换一张
取 消