Regex will not find the characters "\ u2028" unicode

We have a lot of problems with source tracking \ u2028 (line separator) in user data that cause the "unterminated string literal" error in Firefox.

As a result, we are trying to filter it before sending it to the server (and then to the database).

After an extensive googling search and reading by other people, it is clear that I have to filter out these characters before sending them to the database.

Before writing a filter, I tried to find a character to make sure that he can find it using:

var index = content.search("/\u2028/");
alert("Index: [" + index + "]");

I get -1 as a result every time, even when I know that the character is in the content variable (I confirmed with the jUnit Java test on the server side).

Assuming content.replace () will work just like search (), is there something I'm doing wrong or something that I'm missing to find and break these line breaks?

+3
source share
1 answer

The regular expression syntax is invalid. You only use two slashes when using the regex literal. It should be simple:

var index = content.search("\u2028");

or

var index = content.search(/\u2028/); // regex literal

But it really should be done on the server, if you like. Sanitizing JavaScript can be trivially circumvented. This is only useful for user convenience, and I don’t think that accidentally entering a line separator is common.

+5
source

Source: https://habr.com/ru/post/1752392/


All Articles