-1

I'm trying to clear a string of any invalid characters to be set as a directory. Tried a number of methods and this one eventually worked[custom encoding] but now it doesn't, it says "nothing to repeat" in the console. What does that mean? using Chrome.

Here's the code(using random string):

var someTitle = "wa?";
var cleanTitle = cleanTitle(someTitle);

function cleanTitle(title){

    var obstructions = ['\\','/',':','*','?','"','<','>','|'];
    var solutions = [92,47,58,42,63,34,60,62,124];
    var encodedTitle = title;

    for (var obstruction = 0; obstruction < obstructions.length; obstruction++){
            var char = obstructions[obstruction];

            if (encodedTitle.includes(char)){
                var enCode = "__i!__"+solutions[obstruction]+"__!i__";
                var rEx = new RegExp(char,"g");
                encodedTitle = encodedTitle.replace(rEx,enCode);
            }
    }

    console.log("CLEAN: "+title);
    console.log("ENCODED: "+encodedTitle);


    return encodedTitle;
}

Heres the error:

Uncaught SyntaxError: Invalid regular expression: /?/: Nothing to repeat

It points to this line -> var rEx = new RegExp(char,"g");

j08691
  • 204,283
  • 31
  • 260
  • 272
Llama Obama
  • 1
  • 1
  • 3

3 Answers3

2

You need to escape some characters when using them as literals in a regular expression. Among those are most of the characters you have in your array.

Given your function replaces the obstruction characters with their ASCII code (and some wrapping __i!__), I would suggest to make your function a bit more concise, by performing the replacement with one regular expression, and a callback passed to .replace():

function cleanTitle(title){
    return title.replace(/[\\/:*?"<>|]/g, function (ch) {
        return "__i!__"+ch.charCodeAt(0)+"__!i__";
    });
}  

var someTitle = "wh*r* is |his?";
var result = cleanTitle(someTitle);

console.log(result);

...and if you are in an ES6 compatible environment:

var cleanTitle = t=>t.replace(/[\\/:*?"<>|]/g, c=>"__i!__"+c.charCodeAt(0)+"__!i__"); 

var someTitle = "wh*r* is |his?";
var result = cleanTitle(someTitle);

console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
0

The ? is a regex modifier. When you want to look for it (and build a regex with it), you need to escape it. That beeing said, a harmless unuseful escaping doesn't hurt (or makes your other search params useable, as there are many modifiers or reserved chars in it) your other search params. So go with

var char = '\\' + obstructions[obstruction];

to replace them all with a (for the regex) string representation

baao
  • 71,625
  • 17
  • 143
  • 203
0

/?/ is not a valid regex. For it to be a regex, you need /\?/.

Regex here would be awkward, as most of the characters need escaping. Instead, consider using a literal string replacement until it is no longer found:

while( encodedTitle.indexOf(char) > -1) {
    encodedTitle = encodedTitle.replace(char,enCode);
}
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592