I have done something like this
but its not working
can anyone please correct following regex.
/^[a-zA-Z.\s]+$/
You can use
/^[a-zA-Z]*$/
Change the * to + if you don't want to allow empty matches.
References:
Character classes ([...]), Anchors (^ and $), Repetition (+, *)
The / are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
If you want to get only alphabets, remove . from regex. This will match all the alphabets and spaces.
/^[a-zA-Z\s]+$/
I'll also recommend you to use instead of \s
/^[a-zA-Z ]+$/
so that, other space characters(tabs, etc.) will not matched.
Related
I would like to ignore 'regularexpression' but also ignore space and Uppercase so if someone types 'regular expression' or 'reguLarExpression' it will still match and ignore. Can you please help.
^(?!(regularexpression)$)[a-zA-Z](?:[ ()'.\-a-zA-Z]*[a-zA-Z()])
I use this code in parsley.js:
data-parsley-pattern="^(?!(regularexpression)$)[a-zA-Z](?:[ ()'.\-a-zA-Z]*[a-zA-Z()])"
I have a set of words that I want to ignore but they don't have spaces or lower case. So, I need to cover the variations like in the example above.
Judging by your pattern, you want the whole string to only contain letters, spaces, (, ), ', . and - and should start with a letter and end with a letter or parentheses. Beside that, you are trying to negate the match if the string contains regular expression, regularexpression, RegULar ExpressioN, etc.
In parsley.js, you may use both string and regex literal patterns, i.e. data-parsley-pattern="\d+" = data-parsley-pattern="/^\d+$/". Note that string patterns are anchored by the framework automatically, while with the regex literal notation you need to add the anchors to make sure the whole string matches the regex.
As JavaScript regex does not support inline modifiers, you need to use the *regex literal notation with / as delimitiers.
The data-parsley-pattern will look like
data-parsley-pattern="/^(?!.*regular\s*expression)[a-zA-Z](?:[ ()'.a-zA-Z-]*[a-zA-Z()])?$/i"
See the regex demo. Note the /.../i: the i is the case insensitive flag here.
To add more exceptions, keep on adding (?!.*my\s*new\s*phrase), or use an alternation inside a single lookahead, (?!.*(?:regular\s*expression|my\s*new\s*phrase)). Also, use word boundaries if you need to match these phrases as whole words, e.g. (?!.*\b(?:regular\s*expression|my\s*new\s*phrase)\b).
Pattern details
^ - start of string
(?!.*regular\s*expression) - no match if there is regular + 0 or more whitespaces and then expression after any 0+ chars other than line break chars as many as possible
[a-zA-Z] - an ASCII letter
(?:[ ()'.a-zA-Z-]*[a-zA-Z()])? - an optional sequence of
[ ()'.a-zA-Z-]* - 0+ ASCII letters, space, (, ), ', . or -
[a-zA-Z()] - an ASCII letter or ( or )
$ - end of string.
JS demo:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/parsleyjs/2.0.0-rc5/parsley.js"></script>
<form id="parsley" data-parsley-validate>
<input type="text" name="the_name" id="the_id" data-parsley-pattern="/^(?!.*regular\s*expression)[a-zA-Z](?:[ ()'.a-zA-Z-]*[a-zA-Z()])?$/i" required>
<input type="submit" />
</form>
Unfortunately I do not know how to combine these together but someone will know but here are the Regex Patterns I would use.
Whitespace and Character Casing
For the word Regular Expression.
^[A-Za-z.\s_-]+$
(?:regularexpression|regular expression)
It's a bit difficult to guess, maybe this expression might be closer to what you have in mind:
(?i)^(?!(regular\s*expression)$)[a-z](?:[ ()'.a-z-]*[a-z()])$
If you wish to explore/simplify/modify the expression, it's been
explained on the top right panel of
regex101.com. If you'd like, you
can also watch in this
link, how it would match
against some sample inputs.
how to write regular expression allow name with one space and special Alphabets?
I tried with this [a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)* but not working for me,
example string Björk Guðmundsdóttir
You may try something along these lines:
^(?!.*[ ].*[ ])[ A-Za-zÀ-ÖØ-öø-ÿ]+$
The first negative lookahead asserts that we do not find two spaces in the name. This implies that at most one space is present (or no spaces at all). Then, we match any number of alphabets, with most accented letters included. Spaces can also be matched, but the lookahead would already ensure that at most one space can be present.
Demo
Use this one:
[a-zA-Z\u00C0-\u00ff]*[ ]{1}[a-zA-Z\u00C0-\u00ff]*
Answer from other question
I am trying to use regexp to match some specific key words.
For those codes as below, I'd like to only match those IFs at first and second line, which have no prefix and postfix. The regexp I am using now is \b(IF|ELSE)\b, and it will give me all the IFs back.
IF A > B THEN STOP
IF B < C THEN STOP
LOL.IF
IF.LOL
IF.ELSE
Thanks for any help in advance.
And I am using http://regexr.com/ for test.
Need to work with JS.
I'm guessing this is what you're looking for, assuming you've added the m flag for multiline:
(?:^|\s)(IF|ELSE)(?:$|\s)
It's comprised of three groups:
(?:^|\s) - Matches either the beginning of the line, or a single space character
(IF|ELSE) - Matches one of your keywords
(?:$|\s) - Matches either the end of the line, or a single space character.
Regexr
you can do it with lookaround (lookahead + lookbehind). this is what you really want as it explicitly matches what you are searching. you don't want to check for other characters like string start or whitespaces around the match but exactly match "IF or ELSE not surrounded by dots"
/(?<!\.)(IF|ELSE)(?!\.)/g
explanation:
use the g-flag to find all occurrences
(?<!X)Y is a negative lookbehind which matches a Y not preceeded by an X
Y(?!X) is a negative lookahead which matches a Y not followed by an X
working example: https://regex101.com/r/oS2dZ6/1
PS: if you don't have to write regex for JS better use a tool which supports the posix standard like regex101.com
Using Jquery validator plugin in my implementation. Need a regular expression which excludes special characters like , and &.
is there any regular expression for this. also if this special characters are anywhere in the string it should find and throw the error.
You can use regular expressions like this:
[\,\&]
you can add as much as u want to this.
try it out yourself on this site:
http://www.regexr.com/
/[,&]/g
matches , and &.
Demo: https://regex101.com/r/gY0mC3/2#javascript
If you want to search for every special character except letters, numbers and the underscore, use
/\W/g
Demo: https://regex101.com/r/gY0mC3/5#javascript
If you need to include spaces (e.g. a name) use
/[^\w\s]/g
Demo: https://regex101.com/r/gY0mC3/4#javascript
The brackets [] define custom regex classes.
To match a character for only those characters, you can do [\,\&].
To match all except that, you can add a ^, such as [^\,\&].
To match any non-word character, you can use \W (any character not a-z, A-Z, 0-9, or _).
To include an underscore, you can do [\W_].
Keep in mind that whitespaces are represented by \s and that depending on your environment, you may need to escape (add an additional backslash to) your backslashes.
Tried to search for /\,$/ online, but coudnt find anything.
I have:
coords = coords.replace(/\,$/, "");
Im guessing it returns coords string index number. What I have to search online for this, so I can learn more?
/\,$/ finds the comma character (,) at the end of a string (denoted by the $) and replaces it with empty (""). You sometimes see this in regex code aiming to clean up excerpts of text.
It's a regular expression to remove a trailing comma.
That thing is a Regular Expression, also known as regex or regexp. It is a way to "match" strings using some rules. If you want to learn how to use it in JavaScript, read the Mozilla Developer Network page about RegExp.
By the way, regular expressions are also available on most languages and in some tools. It is a very useful thing to learn.
That's a regular expression that finds a comma at the end of a string. That code removes the comma.
// defines a JavaScript regular expression, used to match a pattern within a string.
\,$ is the pattern
In this case \, translates to ,. A backslash is used to escape special characters, but in this case, it's not necessary. An example where it would be necessary would be to remove trailing periods. If you tried to do that with /.$/ the period here has a different meaning; it is used as a wildcard to match [almost] any character (aside for some newlines). So in this case to match on "." (period character) you would have to escape the wildcard (/\.$/).
When $ is placed at the end of the pattern, it means only look at the end of the string. This means that you can't mistakingly find a comma anywhere in the middle of the string (e.g., not after help in help, me,), only at the end (trailing). It also speeds of the regular expression search considerably. If you wanted to match on characters only at the beginning of the string, you would start off the pattern with a carat (^), for instance /^,/ would find a comma at the start of a string if one existed.
It's also important to note that you're only removing one comma, whereas if you use the plus (+) after the comma, you'd be replacing one or more: /,+$/.
Without the +; trailing commas,, becomes trailing commas,
With the +; no trailing comma,, becomes no trailing comma