最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript Regular Expression failing in IE, but working in Chrome & Edge - Stack Overflow

programmeradmin1浏览0评论

Im trying to replace illegal characters from a filename using a regular expression in javascript but it keeps falling over in IE 11 with 'Syntax error in regular expression'. The same code works fine in Chrome and Edge.

String.prototype.replaceAll = function (search, replacement) {
     var target = this;
     return target.replace(search, replacement);
};

var filename = 'test+&+this+again.2016.txt';

filename = filename.replaceAll(new RegExp(/[^a-zA-Z0-9_\-&.]+/, 'g'), '_');

Desired output is

filename = 'test_&_this_again.2016.txt';

Any help would be greatly appreciated.

Thanks

Im trying to replace illegal characters from a filename using a regular expression in javascript but it keeps falling over in IE 11 with 'Syntax error in regular expression'. The same code works fine in Chrome and Edge.

String.prototype.replaceAll = function (search, replacement) {
     var target = this;
     return target.replace(search, replacement);
};

var filename = 'test+&+this+again.2016.txt';

filename = filename.replaceAll(new RegExp(/[^a-zA-Z0-9_\-&.]+/, 'g'), '_');

Desired output is

filename = 'test_&_this_again.2016.txt';

Any help would be greatly appreciated.

Thanks

Share Improve this question asked Apr 22, 2016 at 8:46 MattMatt 4092 gold badges8 silver badges15 bronze badges 3
  • 1 Side question: what is the purpose of your replaceAll function, as it is just calling standard replace one? – sp00m Commented Apr 22, 2016 at 8:48
  • FWIW: /[^a-zA-Z0-9_\-&.]+/ is the same as /[^\w\-&.]+/ – Thomas Ayoub Commented Apr 22, 2016 at 8:57
  • I hope you don't leak this .replaceAll to production code :-O – Andreas Louv Commented Apr 22, 2016 at 9:03
Add a comment  | 

2 Answers 2

Reset to default 10

The point is that RegExp constructor accepting the regex literal object is not supported in all browsers as you see. Use a common code like this:

filename = 'test+&+this+again.2016.txt'; 
filename = filename.replace(/[^a-zA-Z0-9_&.-]+/g, '_');
document.body.innerHTML = filename;

For it to work consistently. When the browsers start complying with the ES6, there won't be any trouble using the regex literal object inside the constructor (source: MDN):

Starting with ECMAScript 6, new RegExp(/ab+c/, 'i') no longer throws a TypeError ("can't supply flags when constructing one RegExp from another") when the first argument is a RegExp and the second flags argument is present. A new RegExp from the arguments is created instead.

Also, I suggest using a regex literal notation since the pattern is not built dynamically. Here is the recommendation from MDN:

The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant...

The constructor of the regular expression object, for example, new RegExp('ab+c'), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

Double escape \\ and a string representation should do it:

filename = filename.replaceAll(new RegExp('[^a-zA-Z0-9_\\-&.]+', 'g'), '_');
发布评论

评论列表(0)

  1. 暂无评论