So I am trying to write a regex to validate a normal sentence with no weird characters other than the basic ones you would see in a sentence (e.g: .,':<>... etc) and that is no longer than 512 characters. I am struggling to figure out how to do this, even after trying to look up the appropriate documentation for it.
The test code with the regex I have right now is below, however this does not work unless I remove all special characters:
const sentence = "This is a test sentence with some special characters ./<>'...";
if (/^[\w]{1,512}$/i.test(sentence)) {
console.log("You provided a valid sentence.");
}
How do I make it so that this regex allows for basic sentence characters?
Also, are there any helpful tools that I can use to create regex's for JavaScript? Thanks in advance.
Edit: I now realize that I need to just add in all the characters that I want to allow, but now I am unsure how to do so without breaking syntax and including the ' and " characters.
const sentence = "This is a test sentence with some special characters ./<>'...";
if (/^[\w~!##$%^&*()_+{}[]:";\'<>?,./]{1,512}$/i.test(sentence)) {
console.log("You provided a valid sentence.");
}
After help from others in the comments of my original post, I managed to get what I wanted with the following code:
const sentence = "This is a test sentence with some special characters ./<>'...";
if (/^[\w~!##$%^&*()_+{}[]:";\'<>?,.\/]{1,512}$/i.test(sentence)) {
console.log("You provided a valid sentence.");
}
Thank you to those who helped me!
Related
I am making a filter for a chat room I own.
I was succesful in having it turn NSFW words into a bunch of symbols and astericks to censor it, but many people bypass it by simply putting a backslash, period, or other symbol/letter after it because I only put in the words without the punctation and symbols. They also come up with a bit more creative methods such as eeeNSFWeee so the filter doesn't count it as a word.
Is there a way to make it so that the filter will select certain characters that form a word in a string and replace them (with or without replacing the extra characters connected to the message)?
The Filter is made in javascript and Socket.io
Filter code:
const array = [
"NSFW",
"Bad Word"
"Innapropiate Word"
];
message = message
.split(" ")
.map((word) => (array.includes(word.toLowerCase()) ? "$#!%" : word))
.join(" ");
For an example if somebody typed "Bad Word" exactly like that (caps are not a problem), it would censor it succesfully.
But if somebody typed "Bad Word." that would be a problem because since it has a period it would count it as a different word, thats what I need fixed.
There are a number of approaches you could take here.
You could use replace() if you just want to remove symbols. For example:
word.replace(/[&\/\\#,+()$~%.`'"!;\^:*?<>{}_\[\]]/g, '')
You could use Regular Expressions in general, which allows you to match on patterns instead of exact string matching.
You could also use more complex fuzzy matching libraries or custom fuzzy matching to accomplish your goal. This post may be helpful.
I want to use following code for password validation of at lest one character,number, upper case and lower case character
function fd3() {
alert("hii");
var v1 = document.getElementById("h3").value,
pass = /^([a-zA-Z0-9_##$%^&*]+$)/;
if(!pass.test(v1)) {
alert("Wrong Password");
}
}
Strictly speaking it does match your example of Abc#123.
Follow this link to see it matching Abc#123
However it doesn't do anything like what you think it does - it will match one or more character in that group - so simply "A" alone will match it.
You need something MUCH more complex like this:
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]).*$
Debuggex Demo of the working example
I would also note that you did not have a requirement for a minimal number of characters so I did not include this in the regex. My list of special characters in this regex is also different from yours, but that can easily be changed by you if that is important.
I would like to allow all Alphanumeric Characters and disallow all Special Characters in a RegEx. But I would like to allow German Umlauts but becouse they are Special Chars too, I can't type them in. I use this Script:
if(website_media_description.match(/[^a-zA-Z0-9]/g)) {
alert('Found Special Char');
}
So when a äöüÄÖÜß is in the variable than I get the alert too. I also tryed this Script:
if(website_media_description.match(/[^a-zA-Z0-9äöüÄÖÜß]/g)) {
alert('Found Special Char');
}
But this also does not work. Can someone please tell me what I am doing wrong?
Thanks :)
my test String comes from an input field, i write "description test 1 öäüÖÄÜß"
Your problem is coming from the fact you haven't considered every character you want in your whitelist.
Let's consider what is actually matched by your test string
"description test 1 öäüÖÄÜß".match(/[^a-zA-Z0-9äöüÄÖÜß]/g);
// [" ", " ", " "]
As we can see, it matched 3 times, and each time was whitespace. So, the solution is to add a space to your whitelist (assuming you don't want to allow tab/return etc).
"description test 1 öäüÖÄÜß".match(/[^a-zA-Z0-9äöüÄÖÜß ]/g);
// null
Your test string now passes the RegExp without a match, which means it is valid in this case.
For some reason I needed to use the unicode representation:
[^a-zA-Z0-9\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00df]`
Thanks to everyone :)
I am trying to validate a password string with javascript and need some help with a regex. I have tried some tutorials, but I think I have some problems understanding how to escape quantifiers and/or metacharacters.
I want to make sure that the password string only contains one or more (max 32) characters from the following spans:
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"012345678901234567890123456789"
"!##%&/(){}[]=?+*^~-_.:,;"
The first three spans are pretty easy, but I can't figure out the last one. Basically my script looks something like this:
var password = "user_input_password";
if (/^[A-Za-z0-9!##$%...]{1,32}$/.test(password)) {
document.write('OK');
} else {
document.write('Not OK');
}
Any help or input is highly appreciated, thanks!
In general, you can escape a meta-character using a backslash \; however, inside a character class, the only ones you have to escape are ] , \ and - (the ^ only has a meaning at the very beginning). Something like [\w!##%&/(){}[\]=?+*^~\-.:,;] will do what you want.
The \w is equal to [A-Za-z0-9_].
So the full test would be something like:
/^[\w!##%&/(){}[\]=?+*^~\-.:,;]{1,32}$/.test(password)
/^[A-Za-z0-9!##%&\/(){}\[\]=?+*^~\-_\.:,;]{1,32}$/
You can also match all characters that are not considered white space (space, newline, tab)
/^[^\s]{1,32}$/.test(password);
To exclude quotes as well (I didn't see them in your example) you can add those in:
/^[^\s'"]{1,32}$/.test(password);
i've already read all tha articles in here wich touch a similar problem but still don't get any solution working. In my case i wanna wrap each word of a string with a span. The words contain special characters like 'äüö...'
What i am doing at the moment is:
var textWrap = text.replace(/\b([a-zA-Z0-9ßÄÖÜäöüÑñÉéÈèÁáÀàÂâŶĈĉĜĝŷÊêÔôÛûŴŵ-]+)\b/g, "<span>$1</span>");
But what happens is that if the äüñ or whatever NON-Ascii character is at the end or at the beginning it also acts like a boundary. Being within a word these characters do't act as a boundary.
'Ärmelkanal' becomes Ä<span>rmelkanal</span> but should be <span>Ärmelkanal</span>
'Käse'works fine... becomes <span>Käse</span>
'diré' becomes <span>dir</span>é but should be <span>diré</span>
Any advice would be very appreciated. I need to do that on clientside :-( BTW did i mention that i hate regular expressions ;-)
Thank You very much!
The problem is that JavaScript recognizes word boundaries only before/after ASCII letters (and numbers/underscore). Just drop the \b anchors and it should work.
result = subject.replace(/[a-zA-Z0-9ßÄÖÜäöüÑñÉéÈèÁáÀàÂâŶĈĉĜĝŷÊêÔôÛûŴŵ-]+/g, "<span>$&</span>");