REgex not working for white space javascript - javascript

I am trying to validate string which can take only alphanumeric values without space.
str.replace(/[^a-zA-Z0-9\u00E0-\u00FC ]+/gi, '')
I used above code but seems still it is taking white space. Did not able to find the right way to fix it.

Remove the space in regexp
str.replace(/[^a-zA-Z0-9\u00E0-\u00FC]+/gi, '')
// ---------------------------------^ here

Related

javascript regex - splitting a text into sentences on . if no number is before it

I am trying to parse a text into sentences, by using:
srt.replace(/(\.+|:|!|\?)(\s|\n|\r|\r\n)/gm, "$1$2|").split("|");
Which works great, but... If a sentence starts with a list number (i.e "1. some words") I get: ['1.', 'some words'].
It's my first time using regex and while I know there's a way to lookbehind I was not able to use it.
How can I change my regex to only split at . if there's no number character before it?
Ended up using str.replace(/(?<!:)(\n)\s*/g, "$1|").replace(/(?<![0-9])(\.+)\s*/g, "$1|").replace(/(\?+|!+)\s*/g, "$1|").split("|")
I am sure there's a prettier way to write this regex, but as a noob - I don't yet know how. This also covers:
1. Not splitting if there's a new line after :
2. Multiple dots, question and exclamation marks
This code is meant to split a text into "ideas", which is why I used the conditions I did, might not be the right logic for a simple "split to sentences" need.

How to match everything except specified characters and strings? Regex

I am building a graph drawer and currently working on the math expression parser. I'm done with most parts but I'm stuck at clearing the input text before parsing it. What I'm trying to achieve now is getting rid of unpermitted characters.
For example, in this text:
5ax+4asxxv+sdflog10aloga(132*43)sin(132)
I want to match everything that is not +,-,*,/,^,(,),ln,log,sin,cos,tan,cot,arcsin,arccos,...
and replace them with "".
so that the output is
5x+4xx+log10log(132*43)sin(132)
I need help with the regex.
Spaces don't matter since I clear them out beforehand.
A little bit tricky - at least I couldn't think of a simple way to do what you ask. The regex would get monstrous.
So I did it the other way around - match what you want to keep, and put it back together.
The regex:
[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot
The code:
var re = /[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot/g,
text = '5ax+4asxxv+sdflog10aloga(132*43)sin(132)arccos(1)';
console.log(text.match(re).join(''));

How to prevent white space only from going to the database?

I have this JavaScript code that only allows users to enter letters and white space. If I allow that, the user can now enter the name as white space only and it will go to the database. Is there a way to prevent the user from entering white space only and force them to add letters?
And an optional question, can I prevent the user from entering two white spaces?
<script>
function lettersOnly(input) {
var regex = /[^a-z & " "]/gi;
input.value = input.value.replace(regex, "");
}
</script>
<input id="fullname" placeholder="fullname" onkeyup="lettersOnly(this)">
You could just leave your code as is and when the user submits the form, you get rid of any whitespace. Something like:
string.replace(/\s/g,'')
The \s character matches ANY number of whitespaces, so that also solves your problem of preventing the user from entering two or more whitespaces.
However, if having whitespace in the name is a real problem, you should consider doing this on the back-end, to prevent the possibility of people bypassing your regex replace and inserting unwanted values into the DB.
Try this regex, and it will allow for one whitespace only. Otherwise, it will be removed.
var regex = /\s\s+/gi;
input.value = input.value.replace(regex, "");
And if you need to remove all whitespaces, you can try
input.value.replace(/\s/g,'')
Just add a following check condition before invoking service.
input.trim() !=== ""
It's possible to pretty easily prevent only whitespace, use .charAt() to check if the first character is whitespace.
if the entire string is whitespace, then your first character is definitely whitespace.
Example:
if (input.value.charAt(0) == " ") {
//Do your things
}
As far as checking whether or not two immediately adjacent spaces are present, you can do that with regEx relatively easily, using an expression like [ ]{2,}.
In all seriousness, you're probably better off using a well-known, predefined form-validation library for the time being.
To avoid only white spaces given as input
Just add the following check condition
If ( input.trim() == "" ){
Expression
}

JS - having difficulties allowing whitespaces with regex

I am having problems allowing white spaces in js with regex.
I am not sure what it is, but I have been searching and not able to find an answer that works.
I am really new to regex, so please be kind for being ignorant of something this simple.
Here is my allowed characters currently:
var validChar = /^[A-Z0-9]+$/i;
If I add a space in it, it will not work: /^[A-Z 0-9]+$/i
What do I have to do in order to make it recognize a space?
Space character in regexp is defined like "\s":
/^[A-Z0-9\s]+$/i
Add white space in last,like this var validChar = /^[A-Z0-9\s]+$/i;

Regex wordwrap with UTF8 characters in JS

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>");

Categories