Hi guys I'm trying to check if user input string contains a space. I'm using http://regexr.com/ to check if my regular expression is correct. FYI new to regex. Seems to be correct.
But it doesn't work, the value still gets returned even if there is a space. is there something wrong with my if statement or am I missing how regex works.
var regex = /([ ])\w+/g;
if (nameInput.match(regex)||realmInput.match(regex)) {
alert('spaces not allowed');
} else {
//do something else
}
Thanks in Advance
This regex /([ ])\w+/g will match any string which contain a space followed by any number of "word characters". This won't catch, for example, a space at the end of the string, not followed by anything.
Try using /\s+/g instead. It will match any occurrence of at least one space (including tabs).
Update:
If you wish to match only a single space this will do the trick: / /g. There's no real need for the brackets and parenthesis, and since one space is enough even the g flag is kind of obsolete, it could have simply been / /.
Your current regex doesn't match 'abc '(a word with space character at the end) . If you want to make sure, you can trim you input before check :).
You can check here https://regex101.com/
The right regex for matching only white space is
/([ ])/g
Related
I'm looking for a regular expression.
Requirement:
I need to select a complete word from a string (word might contain special character or anything). And m pretty close to the solution.
Example:
character-set
Regular expression: (?:^|\s)(cent-er)(?=\s|$)
Result: " character-set" with a leading space.
But i want to remove leading space from the selected word. The word should match exactly i.e if i say character or character- or -set or set it should not get any result.
Any help is much appreciated. Thanks in advance.
It is not exactly what you seem to describe (as far as I could understand, that is), but maybe what you are looking for are word boundaries: \b. Try the regex (parentheses optional):
(\b)(cent-er)(\b)
Other than that, if you have to have a space before the word, then you will have to match it (and then use capturing groups to extract the word without the space), because JavaScript's regex has no lookbehinds.
Can somebody explain what this regular expression does?
document.cookie.match(/cookieInfo=([^;]*).*$/)[1]
Also it would be great if I can strip out the double quotes I'm seeing in the cookieInfo values. i.e. when cookieInfo="xyz+asd" - I want to strip out the double quotes using the above regular expression.
It basically saying grab as many characters that are not semi-colons and that follow after the string 'cookieInfo='
Try this to eliminate the double quotes:
document.cookie.match(/cookieInfo="([^;]*)".*$/)[1]
It searches the document.cookie string for cookieInfo=.
Next it grabs all of the characters which are not ; (until it hits the first semicolon).
[...] set of all characters included inside.
[^...] set of all characters which don't match
Then it lets the RegEx search through all other characters.
.* any character, 0 or more times.
$ end of string (or in some special cases, end of line).
You could replace " a couple of different ways, but rather than stuffing it into the regex, I'd recommend doing a replace on it after the fact:
var string = document.cookie.match(...)[1],
cleaned_string = string.replace(/^"|"$/g, "");
That second regex says "look at the start of the string and see if there's a ", or look at the end of the string and see if there's a ".
Normally, a RegEx would stop after it did the first thing it found. The g at the end means to keep going for every match it can possibly find in the string that you gave it.
I wouldn't put it in the original RegEx, because playing around with optional quotes can be ugly.
If they're guaranteed to always, always be there, then that's great, but if you assume they are, and you hit one that doesn't have them, then you're going to get a null match.
The regular expression matches a string starting with 'cookieInfo=' followed by and capturing 0 or more non-semi-column characters followed by 0 or more 'anythings'.
To strip out the double quotes you can use the regex /"/ and replace it with an empty string.
I'm trying to extract (potentially hyphenated) words from a string that have been marked with a '#'.
So for example from the string
var s = '#moo, #baa and #moo-baa are writing an email to a#bc.de'
I would like to return
['#moo', '#baa', '#moo-baa']
To make sure I don't capture the email address, I check that the group is preceded by a white-space character OR the beginning of the line:
s.match(/(^|\s)#(\w+[-\w+]*)/g)
This seems to do the trick, but it also captures the spaces, which I don't want:
["#moo", " #baa", " #moo-baa"]
Silencing the grouping like this
s.match(/(?:^|\s)#(\w+[-\w+]*)/g)
doesn't seem to work, it returns the same result as before. I also tried the opposite, and checked that there's no \w or \S in front of the group, but that also excludes the beginning of the line. I know I could simply trim the spaces off, but I'd really like to get this working with just a single 'match' call.
Anybody have a suggestion what I'm doing wrong? Thanks a lot in advance!!
[edit]
I also just noticed: Why is it returning the '#' symbols as well?! I mean, it's what I want, but why is it doing that? They're outside of the group, aren't they?
As far as I know, the whole match is returned from String.match when using the "g" modifier. Because, with the modifier you are telling the function to match the whole expression instead of creating numbered matches from sub-expressions (groups). A global match does not return groups, instead the groups are the matches themselves.
In your case, the regular expression you were looking for might be this:
'#moo, #baa and #moo-baa are writing an email to a#bc.de'.match(/(?!\b)(#[\w\-]+)/g);
You are looking for every "#" symbol that doesn't follow a word boundary. So there is no need for silent groups.
If you don't want to capture the space, don't put the \s inside of the parentheses. Anything inside the parentheses will be returned as part of the capture group.
For some reason, I can't seem to find a good answer for this one.
I have been trying to escape out the caret (\^), and to use the hex, octal, and other codes for the character using \xdd, \dddd, etc...
But my replace regexp won't replace the caret (^) with anything. It seems to simply break the expression.
Here is the code I am using:
var field,myExp;
// \x5E is supposed to represent the caret in Hex...
myExp = / *[^a-z^A-Z^0-9\s\x5E]/gi;
field = field.replace(myExp,"");
alert(field);
Help!
The code snippet you gave is rather confusing, but based on the title of the question, if you just want to replace the character ^ with something else, that can be achieved like this...
var str1 = "test^123";
var str2 = str1.replace(/\^/g, "\x005E");
alert(str2);
A character group beginning with ^ is an exclusionary group, and will match every character that isn't in the [].
If you're trying to remove any letter, number, or ^, change the regex to
myExp = / *[a-zA-Z0-9^\s]/gi;
When you have the caret as the first character in a [] set, it means "not" - but only at the start. So your regexp means "(spaces followed by) anything that's not a-z, or caret, or A-Z, or caret, or 0-9 etc". Remove all but the first caret and you may have more luck :-)
I found the answer, but you guys all helped me get there. Thanks!
I think what was happening was that my exlude (^) was used too many times and so was creating an exclusion of my exclusionary groups... Since there were no separators between the groups, the first one does the trick.
ORIGINAL:
repExp = / *[^a-z^A-Z^0-9]/gi;
FINAL REGEXP:
repExp = / *[^a-zA-Z0-9]/gi;
The above filters out anything that is not a leter (a-zA-Z) or number (0-9) from a string.
Thanks, people!
P.S. The space after the initial "/" is there because for some reason, Dreamweaver sees it as the beginning of a comment. :-(
Are you trying to replace or keep all a-z, A-Z, 0-9, whitespace, and carats?
If you're trying to keep them, only use one ^ at the very beginning of the expression like so:
[^a-zA-Z0-9\s^]
If you're trying to replace them all including carat, use:
[a-zA-Z0-9^\s]
Edit (updated answer in response to comment):
Use
[^a-zA-Z0-9]
to match and replace all characters that are not a-z, A-Z, 0-9.
Note: You should use the same expression server-side to validate these form fields, as people could have javascript disabled, or just mess with the POST value to mess with your database.
How do I remove numbers from a string using Javascript?
I am not very good with regex at all but I think I can use with replace to achieve the above?
It would actually be great if there was something JQuery offered already to do this?
//Something Like this??
var string = 'All23';
string.replace('REGEX', '');
I appreciate any help on this.
\d matches any number, so you want to replace them with an empty string:
string.replace(/\d+/g, '')
I've used the + modifier here so that it will match all adjacent numbers in one go, and hence require less replacing. The g at the end is a flag which means "global" and it means that it will replace ALL matches it finds, not just the first one.
Just paste this into your address bar to try it out:
javascript:alert('abc123def456ghi'.replace(/\d+/g,''))
\d indicates a character in the range 0-9, and the + indicates one or more; so \d+ matches one or more digits. The g is necessary to indicate global matching, as opposed to quitting after the first match (the default behavior).