JS - having difficulties allowing whitespaces with regex - javascript

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;

Related

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(''));

Replace string if the string is not part of a larger string using regex in javascript

I'm trying to write a function which would Uppercase some words in a textarea but I'm having trouble, as these words could be part of other words and I only want to uppercase them when they are standing alone.
E.g.: I want to uppercase OR but only when not in a word like befORe. These words can however be written in a multitude of ways, like at the start of a line, between one or more whitespace characters or in something like this: (somethingsomething)OR(somethingelse) and probably even more, depending on the way other people write their query code.
I had the idea of working with regex, but I'm not skilled enough to work out every possibility of writing these words.
I have no way of changing the textarea to other, better plugins or editors as it's already part of a functioning script.
Any helpful idea would be greatly appreciated.
EDIT: One additional question
I got really helpful answers to my question, Thank you everyone. I do have an additional question though. What if, other then checking that it's not part of a word, I also want to check that it's not preceded or followed by a . period. I tried adding (?!\.) at the beginning of it, but that doesN't work at all. Is there a way to check for that as well?
//EDIT: Never mind, I figured it out. it's \b(?!\.)something(?!\.)\b for the check at the start and end of the word.
Matching on a word boundary \b may suit:
'foo or bar'.replace(/\bor\b/, 'OR'); // foo OR bar
You can also provide a function to replace, so:
'foo or bar'.replace(/\b(or)\b/, function(m){return m.toUpperCase()})); // foo OR bar
which might be good for cases where you have multiple possible matches:
'foo or bar and fum'.replace(/\b(or|and)\b/ig, function(m){return m.toUpperCase()}); // foo OR bar AND fum
Something like this? ( °_• )
var txt="or some text or before (.)or(.) anywhere Or";
console.log(txt.replace(/\bor\b/gi,"OR"));
// "OR some text OR before (.)OR(.) anywhere OR"
Something like this:
'(something)or(somethingelse) before or oR Or OR oreos thor'.replace([^\w]([oO][rR])[^\w], 'OR');

replacing special characters with a specific special character

I have a string which is exactly like this...
R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%2fSka,
I have tried enough to remove the special characters before they reach the browser...but not going anywhere..so planing to rely on my old and trusted friend "javascript" I want it to read
R&B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae&Ska,
I know this can be done through regular expression which I am just not able to figure it out. How would I write the expression?
Any help would be highly appreciated
You may try using:
decodeURIComponent("R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%26Ska")
//^prints^ "R&B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae&Ska"
Look at these answers:
Regex to remove all special characters from string?
They layout a regex that will remove everything EXCEPT those characters you want to allow, this is safer then removing a list of %26,%2f, etc.
For example...
[^0-9a-zA-Z, ]+ would allow all letters, numbers, commas and whitespace.
[^0-9a-zA-Z]+ would be only letters and numbers
The other answers are probably pointing you in a better direction... if it means fixing the string before it gets to the client.
Probably you need decodeURIComponent() function.
<script>
var decodedString = decodeURIComponent('R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%2fSka');
</script>
http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

Extending an existing regex to drop punctuation after URL links

I have an existing replace that matches http within a text string and creates a working URL from the text.
Working Example:
var Text = "Visit Gmail at http://gmail.com"
var linkText = Text.replace(/http:\/\/\S+/gi, '$&');
document.write(linkText);
Output:
Visit Gmail at http://gmail.com
Problem:
The problem arises when the link appears at the end of a sentence and the punctuation incorrectly becomes appended to the end of the URL.
Can someone advise on a way of extending my regex (or maybe adding a second replacement after this has been transformed) to overcome this?
I think the right answer will include adding something along the lines of /\W$/g to my original regex, but I can't see how this can be applied to just one word within the whole string.
As always, very grateful for any help.
Thanks,
Pete
Examples of problem links
http://gmail.com/.
http://gmail.com,
http://gmail.com/?
http://gmail.com!
All of these should resolve the link to http://gmail.com
Note how some could end in a slash then punctuation and others with punctuation directly after the domain name.
Try
/http:\/\/(.(?![.?] |$))*/
My logic is, if the last char is a dot, or question mark followed by either a space or end of string, you don't need it.
var Text = "Visit Gmail at http://gmail.com"
var linkText = Text.replace(/http:\/\/(.(?![.?](?:\s|$)))*./gi, '$&');
document.write(linkText);
Gives
"Visit Gmail at http://gmail.com"
Edit:
This may be better (it doesn't match white space now)
http:\/\/(.(?!(?:[.?](?: |$))))*.
Why not just use a negative character class?
/http://\S+[^.,?!]/gi
You could account for trailing unwanted characters, whether stripping them or not.
The replacement for both is capture buffer 1: <a href="$1">$1<\/a>
This also asumes you can do lookbehind. though I'm not sure if client side JS can do lookbehind assertions.
Strip unwanted chars
/(http:\/\/\S+)(?<![\/.,?!])[\/.,?!]*/
Or, leave unwanted characters
/(http:\/\/\S+)(?<![\/.,?!])/
Alternate, using lookahead
Strip
/(http:\/\/\S+?(?=[\/.,?!]+(?:\s|$)|\s|$))[\/.,?!]*/
Leave
/(http:\/\/\S+?(?=[\/.,?!]+(?:\s|$)|\s|$))/

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