JS: how to remove "spammy invisible chars"? [duplicate] - javascript

This question already has answers here:
Regular expression for all printable characters in JavaScript
(5 answers)
How to replace non-printable unicode characters (Javascript)
(3 answers)
Closed 2 years ago.
Some user is flooding with some chars that bypass regex filters.
when I paste that chars into UTF8 editor, they look same (except the flood version is not selectable completly: it seems to be some invisible chars inserted
And when you switch to ANSI encodage, you clearly see the difference of the 2 words
liebehomo
lâ€iâ€ebâ€ehâ€oâ€mo
When I paste that spammy word into developer tool, I get
s.length gives 14 and not 9 !
So my question is: how would it be possible to filter these spammy words that contains some strange chars ?

Probably as simple as replacing any non-printable character first:
string = string.replace(/[^ -~]+/g, "");
document.getElementById('demo').addEventListener('input', function(e) {
e.target.innerHTML = e.target.innerHTML.replace(/[^ -~]+/g, "");
console.log(e.target.innerHTML);
});
<textarea id="demo"></textarea>

Related

Javascript regex throwing syntax error in edge [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
I have a simple regex I'm using and that works perfectly in chrome but edge throws a syntax error, ths is the line :
var html=text.match(/^<div.+\/div>$/ims);
I don't see the problem.
Because /s flag is not supported, use:
var html=text.match(/^<div[\s\S]+\/div>$/im);
Basically you want to match all characters with a new line character
You can this regex please:-
text.match(/^<div>.+\n*.*<\/div>/)

Javascript Regex 3 times # symbol [duplicate]

This question already has answers here:
Count the number of occurrences of a character in a string in Javascript
(39 answers)
Closed 4 years ago.
I'm trying to create a Giveaway Picker for Instagram. The Rules for the Giveaway are: tag 3 Friends in the comments.
A regular comment should now be #pers1 #pers2 #pers3. How can I check if there are at least 3 # symbols in the comment.
I alredy loaded all comments into the variable comments.
I think it would work with Regex. But I'm not so good in regex. Does anyone have a working regex for this? THX.
If you want to use a regular expression, you can use a global match and test the length of the result:
const numberOfAts = str => str.match(/#/g).length;
console.log(numberOfAts('#pers1 #pers2 #pers3'));
console.log(numberOfAts('#pers1 #pers2 #pers3 #pers4'));
console.log(numberOfAts('#pers1 #pers2'));

Get text between tags regex javascript [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I am trying to get the text between the two tags which also handles multiple lines.I have managed to do it in PHP regex but i am stuck on the javascript one.
<any_tag>random text
dffdffdfdfdfdfdfdfd
dfdfdfdfdfdfdfdfdf
</any_tag>
I want to get the text between <any_tag> and </any_tag>
This is the regex for php
https://regex101.com/r/tQ1bX7/2
Now i am trying to achieve to same in javascript
Give try on following :
/[^(<any_tag>)](?:.*(\r?\n?).*)*(?=\<\/any_tag\>)/gi
Explination :
[^(<any_tag>)] match a single character not present in the list below
(<any_tag>) a single character in the list (<any_tg>) literally (case insensitive)

Replace string including asterisk in Javascript [duplicate]

This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a

Parsing JSON containing new line characters [duplicate]

This question already has answers here:
How do I handle newlines in JSON?
(10 answers)
Closed 8 years ago.
In my website I try to convert a string to JSON which contains a newline.
JSON.parse('{"hallo":"line1\r\nline2","a":[5.5,5.6,5.7]}');
This produces an "Unexpected token" error. Do I need to escape that somehow?
Yes, you should escape both \n and \r as they belong to the list of control characters. Full list of characters that need to be escaped can be found here. Your code would be
obj = JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');
JSFiddle: link
Try:
JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');

Categories