Unescaping multiple values in javascript - javascript

Here is the code to escape special characters:
function escapeRegExp(string){
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1");
}
How can I unescape the special characters to get the original strings?
I am getting confused with use of / and //.

That's pretty easy, you can use another function that removes the \ characters.
// Use this to escape
function escapeRegExp(string){
return string.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g, "\\$1");
}
// And this to unescape
function unescapeRegExp(string) {
return string.replace(/\\([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g, "$1")
}
// EXAMPLE:
escapeRegExp(".?[]");
> "\.\?\[\]"
unescapeRegExp("\.\?\[\]");
> ".?[]"
PS: I corrected your original function, the regular expression was wrong.

I guess, you've take this from MDN.
All you need to revert escaping is to remove every odd occurence of \ character.
function unescapeRegExp(string) {
return string.replace(/\\(.)/g, '$1');
}

Related

Wrong result when replacing with regex

I'm replacing a sub-string using replace function and regex expression.
However after character escape and replacement, I still have an extra '/' character. I'm not really familiar with regex can someone guide me.
I have implemented the escape character function found here: Is there a RegExp.escape function in Javascript?
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
const latexConversions = [
["\\cdot", "*"],
["\\right\)", ")"],
["\\left\(", "("],
["\\pi", "pi"],
["\\ln\((.*?)\)", "log($1)"],
["stdev\((.*?)\)", "std($1)"],
["stdevp\((.*?)\)", "std(\[$1\], \"uncorrected\")"],
["mean\((.*?)\)", "mean($1)"],
["\\sqrt\((.*?)\)", "sqrt($1)"],
["\\log\((.*?)\)", "log10($1)"],
["\(e\)", "e"],
["\\exp\((.*?)\)", "exp($1)"],
["round\((.*?)\)", "round($1)"],
["npr\((.*?),(.*?)\)", "($1!/($1-$2)!)"],
["ncr\((.*?),(.*?)\)", "($1!/($2!($1-$2)!))"],
["\\left\|", "abs("],
["\\right\|", ")"],
];
RegExp.escape = function (s) {
var t = s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
return t;
};
mathematicalExpression = "\\sqrt( )"
//Problem is here
mathematicalExpression = mathematicalExpression.replace(new RegExp(RegExp.escape(latexConversions[8][0]), 'g'), latexConversions[8][1]);
//Works
mathematicalExpression2 = mathematicalExpression.replace(/\\sqrt\((.*?)\)/g, "sqrt($1)");
alert("what I got: "+mathematicalExpression); // "\sqrt()"
alert("Supposed to be: "+ mathematicalExpression2); // "sqtr()"
I have a live example here: https://jsfiddle.net/nky342h5/2/
There are several misconceptions regarding the string literal "\\sqrt\((.*?)\)":
This string in raw characters is: \sqrt((.*?)). Note how there is no difference between the two opening parentheses: the backslash in the string literal was not very useful. In other words, "\(" === "("
Both opening parentheses will be escaped by RegExp.escape
Points 1 and 2 are equally true for the closing parentheses, for the dot, the asterisk and the question mark: they will be escaped by RegExp.escape.
In short, you have no way to distinguish that a character is intended as a literal or as a regex special symbol -- you are escaping all of them as if they were intended as literal characters.
The solution:
Since you already are encoding regex specific syntax in your strings (like (.*?)), you might as well use regex literals instead of string literals.
In the case you highlighted, instead of this:
["\\sqrt\((.*?)\)", "sqrt($1)"]
...use this:
[/\\sqrt\((.*?)\)/g, "sqrt($1)"]
And let your code do:
mathematicalExpression = mathematicalExpression.replace(...latexConversions[8]);
Alternative
If for some reason regex literals are a no-go, then define your own special syntax for (.*?). For instance, use the symbol µ to denote that particular regex syntax.
Then your array pair would look like this:
["\\sqrt(µ)", "sqrt($1)"],
...and code:
mathematicalExpression = mathematicalExpression.replace(
new RegExp(RegExp.escape(latexConversions[8][0]).replace(/µ/g, '(.*?)'), 'g'),
latexConversions[8][1]
);
Note how here the (.*?) is introduced in the string after RegExp.escape has done its job.
extra \ rather than escaping everything
replace ["\\sqrt\((.*?)\)", "sqrt($1)"], with ["\\\\sqrt\((.*?)\)", "sqrt($1)"],
and replace the final replace with
mathematicalExpression = mathematicalExpression.replace(new RegExp((latexConversions1[8][0]), 'g'), latexConversions1[8][1]);

Regex return undefined in a JavaScript String

I have a little code snippet where I use Regular Expressions to rip off punctuation, numbers etc from a string. I am getting undefined along with output of my ripped string. Can someone explain whats happening? Thanks
var regex = /[^a-zA-z\s\.]|_/gi;
function ripPunct(str) {
if ( str.match(regex) ) {
str = str.replace(regex).replace(/\s+/g, "");
}
return str;
}
console.log(ripPunct("*#£#__-=-=_+_devide-00000110490and586#multiply.edu"));
You should pass a replacement pattern to the first replace method, and also use A-Z, not A-z, in the pattern. Also, there is no point to check for a match before replacing, just use replace directly. Also, it seems the second chained replace is redundant as the first one already removes whitespace (it contains \s). Besides, the |_ alternative is also redundant since the [^a-zA-Z\s.] already matches an underscore as it is not part of the symbols specified by this character class.
var regex = /[^a-zA-Z\s.]/gi;
function ripPunct(str) {
return str.replace(regex, "");
}
console.log(ripPunct("*#£#__-=-=_+_devide-00000110490and586#multiply.edu"));

Why do I get blank %3 character when trying to encode reserved characters of a URI Component in javascript?

Here is the situation presented as-is in the fiddle:
http://jsfiddle.net/am4ph9ut
function encodeAllCharacters(){
var result = "%60~!%40%23%24%25%26*()_%2B-%3D%7B%7D%7C%5B%5D%3B%3A'%22%2F.%2C%3C%3E%3F%60";
result = result.replace(/[!'()*-_.~]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
return result;
}
This is a simplified version of the fixedEncodeURIComponent method explained in this page:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
I am not sure why I get %3 added after every character after the regexp is executed.
Your regular expression uses - inside the character class (ie. the square brackets) which means all the characters between the one on the left to the one on the right of the - (inclusive), eg. [c-f] will match c,d,e,f.
To match against the character - itself use the backslash to escape it:
result = result.replace(/[!'()*\-_.~]/g, function(c) {

Javascript replace() method not working

I have this URL :
http://example.com/example/sample/example.jpg
I want to have this :
http:\ /\ /example.com\ /example\ /sample\ /example.jpg
I wrote this code :
function addslashes(str) {
return str.replace('/', '\/');
}
var url = http://example.com/example/sample/example.jpg
var t = addslashes(url);
alert(t);
As an alert, I still get the old URL. What's wrong with this code?
Thanks.
If you want to print \ you must escape it with another backslash.
function addslashes(str) {
return str.replace(/\//g, '\\/');
}
Also, if you want the replace function to replace all occurrences, you must pass a regex with a g modifier instead of a string. If you pass a string it will only replace the first match and then end but with the modifier it will find all matches.
try this code fiddle:
function addslashes(str) {
return str.replace(/\//g, '\\/');
}
you need to add the g to set it to global, to replace all the '/' and in the replacing string you need to add '\'.
You have to add an additinal backslash to escape it right.
With replace you would only replace the first match. You can also use Regular expression as you can see on the other posts. But you can also use it with simple split and join functions
function addslashes(url) {
url.split('/').join('\\/');
}
Demo

Replace array of chars in a string

I'm looking for a function that does replaceAll with any start and endcharacter.
I know I can use the regex notation:
string=string.replace(/a/g,"b");
However, because the searched char is in a regex, I sometimes need to escape that character and sometimes not, which is annoying if I want to do this for a full list of chars
convertEncoding= function(string) {
var charMap= {'"':""",'&':"&",...}
for (startChar in charMap) {
endChar=charMap[startChar];
string= string.replaceAll(startChar,endChar);
}
}
Is they a good way to write that function replaceAll, without doing a for loop and using String.replace() (eg the naive way) ?
you can use escape the RegExp special characters in the strings such as described here https://stackoverflow.com/a/6969486/519995:
and then you can use the regexp global replace
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
for (startChar in charMap) {
endChar=charMap[startChar];
result = string.replace(RegExp(escapeRegExp(startChar), 'g'), endChar);
}

Categories