Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript replace() not replacing text containing literal \r\n strings

Using this bit of code trims out hidden characters like carriage returns and linefeeds with nothing using javascript just fine:

value = value.replace(/[\r\n]*/g, "");

but when the code actually contains \r\n text what do I do to trim it without affecting r's and n's in my content? I've tried this code:

value = value.replace(/[\\r\\n]+/g, "");

on this bit of text:

{"client":{"werdfasreasfsd":"asdfRasdfas\r\nMCwwDQYJKoZIhvcNAQEBBQADGw......

I end up with this:

{"cliet":{"wedfaseasfsd":"asdfRasdfasMCwwDQYJKoZIhvcNAQEBBQADGw......

Side note: It leaves the upper case versions of R and N alone because I didn't include the /i flag at the end and thats ok in this case.

What do I do to just remove \r\n text found in the string?

like image 608
Vince Avatar asked Nov 16 '13 20:11

Vince


People also ask

What is $1 in replace JavaScript?

In your specific example, the $1 will be the group (^| ) which is "position of the start of string (zero-width), or a single space character". So by replacing the whole expression with that, you're basically removing the variable theClass and potentially a space after it.

What is replace () in JavaScript?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.

Does string replace replacing all occurrences?

The String type provides you with the replace() and replaceAll() methods that allow you to replace all occurrences of a substring in a string and return the new version of the string.

How do you replace a certain part of a string JavaScript?

replace() is an inbuilt method in JavaScript which is used to replace a part of the given string with some another string or a regular expression. The original string will remain unchanged. Parameters: Here the parameter A is regular expression and B is a string which will replace the content of the given string.


1 Answers

If you want to match literal \r and literal \n then you should use the following:

value = value.replace(/(?:\\[rn])+/g, "");

You might think that matching literal \r and \n with [\\r\\n] is the right way to do it and it is a bit confusing but it won't work and here is why:

Remember that in character classes, each single character represents a single letter or symbol, it doesn't represent a sequence of characters, it is just a set of characters.

So the character class [\\r\\n] actually matches the literal characters \, r and n as separate letters and not as sequences.

Edit: If you want to replace all carriage returns \r, newlines \n and also literal \r and '\n` then you could use:

value = value.replace(/(?:\\[rn]|[\r\n]+)+/g, "");

About (?:) it means a non-capturing group, because by default when you put something into a usual group () then it gets captured into a numbered variable that you can use elsewhere inside the regular expression itself, or latter in the matches array.

(?:) prevents capturing the value and causes less overhead than (), for more info see this article.

like image 151
Ibrahim Najjar Avatar answered Oct 06 '22 09:10

Ibrahim Najjar