Issue with JS regular expression - javascript

I'm trying to create a regular expression in order to check some text inserted in a textarea. Basically, when typing I check the number of words inserted. As special chars, only commas and full stop are allowed.
The problem is, for example, when I type word,anotherword my regex recognises only one word instead of two. I cannot find a good regex for it.
My current regex is:
val.match(/\S+[A-Za-z]/g
What shall I add? Thanks a lot!

Since \S matches any non-whitespace characters, and a comma, too, you should be using
val.match(/\w+/g)
A \w matches word characters, those in A-Z, a-z, 0-9 ranges and a _ (underscore).

Do you want to extract the words, or check if the entered text is of the correct format?
If the first, use js Array.split(",") and check that each word is ok.
Otherwise, I think this refer should do it:
val.match(/^(\w+\,?)+$/);
Use the ^ and $ to make sure it starts and end around the found correct format.

Related

Match exact word and remove leading space in regular expression

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 it be done with regex?

Having the following regex: ([a-zA-Z0-9//._-]{3,12}[^//._-]) used like pattern="([a-zA-Z0-9/._-]{3,12}[^/._-])" to validate an HTML text input for username, I wonder if is there anyway of telling it to check that the string has only one of the following: ., -, _
By that I mean, that I'm in need of regex that would accomplish the following (if possible)
alex-how => Valid
alex-how. => Not valid, because finishing in .
alex.how => Valid
alex.how-ha => Not valid, contains already a .
alex-how_da => Not valid, contains already a -
The problem with my current regex, is that for some reason, accepts any character at the end of the string that is not ._-, and can't figure it out why.
The other problem, is that it doesn't check to see that it contains only of the allowed special characters.
Any ideas?
Try this one out:
^(?!(.*[.|_|-].*){2})(?!.*[.|_|-]$)[a-zA-Z0-9//._-]{3,12}$
Regexpal link. The regex above allow at max one of ., _ or -.
What you want is one or more strings containing all upper, lower and digit characters
followed by either one or none of the characters in "-", ".", or "_", followed by at least one character:
^[a-zA-Z0-9]+[-|_|\.]{0,1}[a-zA-Z0-9]+$
Hope this will work for you:-
It says starts with characters followed by (-,.,_) and followed and end with characters
^[\w\d]*[-_\.\w\d]*[\w\d]$
Seems to me you want:
^[A-Za-z0-9]+(?:[\._-][A-Za-z0-9]+)?$
Breaking it down:
^: beginning of line
[A-Za-z0-9]+: one or more alphanumeric characters
(?:[\._-][A-Za-z0-9]+)?: (optional, non-captured) one of your allowed special characters followed by one or more alphanumeric characters
$: end of line
It's unclear from your question if you wanted one of your special characters (., -, and _) to be optional or required (e.g., zero-or-one versus exactly-one). If you actually wanted to require one such special character, you would just get rid of the ? at the very end.
Here's a demonstration of this regular expression on your example inputs:
http://rubular.com/r/SQ4aKTIEF6
As for the length requirement (between 3 and 12 characters): This might be a cop-out, but personally I would argue that it would make more sense to validate this by just checking the length property directly in JavaScript, rather than over-complicating the regular expression.
^(?=[a-zA-Z0-9/._-]{3,12}$)[a-zA-Z0-9]+(?:[/._-][a-zA-Z0-9]+)?$
or, as a JavaScript regex literal:
/^(?=[a-zA-Z0-9\/._-]{3,12})[a-zA-Z0-9]+(?:[\/._-][a-zA-Z0-9]+)?$/
The lookahead, (?=[a-zA-Z0-9/._-]{3,12}$), does the overall-length validation.
Then [a-zA-Z0-9]+ ensures that the name starts with at least one non-separator character.
If there is a separator, (?:[/._-][a-zA-Z0-9]+)? ensures that there's at least one non-separator following it.
Note that / has no special meaning in a regex. You only have to escape it if you're using a regex literal (because / is the regex delimiter), and you escape it by prefixing with a backslash, not another forward-slash. And inside a character class, you don't need to escape the dot (.) to make it match a literal dot.
The dot in regex has a special meaning: "any character here".
If you mean a literal dot, you should escape it to tell the regex parser so.
Escape dot in a regex range

(js)regular expression for matching a words only

I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/

Length of regex expression with specific values

I have a javascript regex against a text area in an html form. Here's the regex:
regex = /[EePp]+/
I would like to also use the regex to check the length of the string in the text area and to have it to be limited to a single character.
I tried
regex = /[EePp]{1}/
And it validates regex to only those characters, but still allows strings of more than one character in the text area:
<input type='text' onkeypress='validate(event)'>
Is it possible to do that through the regex?
If you add anchors, ^ and $ at the start and end of your regex, this will limit it to matching only the pattern and nothing else against the full extent of what is being searched.
So, /^[EePp]{1}$/ -- That says [EePP] at the very beginning of what is being searched,^, and there is nothing between it and the end, $, of what is being searched.
Turns out that in this case you don't need the {1}, because the anchors are telling it exactly how far the match extends. So:
/^[EePp]$/
should do it.

How can I remove the caret(^) from a string using a RegExp in Javascript?

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.

Categories

Resources