I am looking for a regex pattern that ensures the user puts in a single lower case word with only letters of the alphabet. Basically they are picking a subdomain. Thanks in advance
The character class [a-z] describes one single character of the alphabet of lowercase letters a–z. If you want if an input does only contain characters of that class, use this:
^[a-z]+$
^ and $ mark the start and end of the string respectively. And the quantifier + allows one or more repetitions of the preceding expression.
^[a-z]+$ Will find one and only one lower-case word, with no spaces before or after the word.
/^[a-z]+$/
make sure you aren't using 'i' after the last slash
/[a-z]+/
if you are searching for any words within the context
If you want to find all occurrences of lowercase-only ASCII-char words, you can use
text.match(/\b[a-z]+\b/g)
See the regex demo.
Details:
\b - a word boundary
[a-z]+ - one or more (+) lowercase ASCII letters
\b - a word boundary
The g flag makes it extract all occurrences.
See the JavaScript demo:
const text = "123456789 Ticket number (CO2) text";
console.log(text.match(/\b[a-z]+\b/g));
Related
I want a JS regex that only matches names with capital letters at the beginning of each word and lowercase letters thereafter. (I don't care about technical accuracy as much as visual consistency — avoiding people using, say, all caps or all lower cases, for example.)
I have the following Regex from this answer as my starting point.
/^[a-z ,.'-]+$/gmi
Here is a link to the following Regex on regex101.com.
As you can see, it matches strings like jane doe which I want to prevent. And only want it to match Jane Doe instead.
How can I accomplish that?
Match [A-Z] initially, then use your original character set afterwards (sans space), and make sure not to use the case-insensitive flag:
/^[A-Z][a-z,.'-]+(?: [A-Z][a-z,.'-]+)*$/g
https://regex101.com/r/y172cv/1
You might want the non-word characters to only be permitted at word boundaries, to ensure there are alphabetical characters on each side of, eg, ,, ., ', and -:
^[A-Z](?:[a-z]|\b[,.'-]\b)+(?: [A-Z](?:[a-z]|\b[,.'-]\b)+)*$
https://regex101.com/r/nP8epM/2
If you want a capital letter at the beginning and lowercase letters following where the name can possibly end on one of ,.'- you might use:
^[A-Z][a-z]+[,.'-]?(?: [A-Z][a-z]+[,.'-]?)*$
^ Start of string
[A-Z][a-z]+ Match an uppercase char, then 1+ lowercase chars a-z
[,.'-]? Optionally match one of ,.'-
(?: Non capturing group
[A-Z][a-z]+[,.'-]? Match a space, then repeat the same pattern as before
)* Close group and repeat 0+ times to also match a single name
$ End of string
Regex demo
Here's my solution to this problem
const str = "jane dane"
console.log(str.replace(/(^\w{1})|(\s\w{1})/g, (v) => v.toUpperCase()));
So first find the first letter in the first word (^\w{1}), then use the PIPE | operator which serves as an OR in regex and look for the second block of the name ie last name where the it is preceded by space and capture the letter. (\s\w{1}). Then to close it off with the /g flag you continue to run through the string for any iterations of these conditions set.
Finally you have the function to uppercase them. This works for any name containing first, middle and lastname.
I would like to use regex in javascript to put a zero before every number that has exactly one digit.
When i debug the code in the chrome debugger it gives me a strange result where only every second match the zero is put.
My regex
"3-3-7-3-9-8-10-5".replace(/(\-|^)(\d)(\-|$)/g, "$10$2$3");
And the result i get from this
"03-3-07-3-09-8-10-05"
Thanks for the help
Use word boundaries,
(\b\d\b)
Replacement string:
0$1
DEMO
> "3-3-7-3-9-8-10-5".replace(/(\b\d\b)/g, "0$1")
'03-03-07-03-09-08-10-05'
Explanation:
( starting point of first Capturing group.
\b Matches between a word character and a non word character.
\d Matches a single digit.
\b Matches between a word character and a non word character.
) End of first Capturing group.
You can use this better lookahead based regex to prefix 0 before every single digit number:
"3-3-7-3-9-8-10-5".replace(/\b(\d)\b(?=-|$)/g, "0$1");
//=> "03-03-07-03-09-08-10-05"
Reason why you're getting alternate prefixes in your regex:
"3-3-7-3-9-8-10-5".replace(/(\-|^)(\d)(\-|$)/g, "$10$2$3");
is that rather than looking ahead you're actually matching hyphen after the digit. Once a hyphen has been matched it is not matched again since internal regex pointer has already moved ahead.
use a positive lookahead to see the one digit numbers :
"3-3-7-3-9-8-10-5".replace(/(?=\b\d\b)/g, "0");
I am looking for regular expression in JavaScript that will help me to return all first characters of each word in Uppercase:
"To do string" => "TDS"
Most important part is to return new string from old.
Easy, with only two functions:
result = subject.replace(/\B[a-z]+\s*/g, "").toUpperCase();
In the demo, look at the substitutions at the bottom. That's the effect of the regex replacement before the toUpperCase()
How it Works
\B matches a position that is not a word boundary, in other words, a spot between two letters.
[a-z]+ matches as many letters as possible
\s* matches optional spaces
we replace all this with the empty string
we convert to upper case
"To do string".match(/\b(\w)/g).join('').toUpperCase(); will give you the desired result.
I would like to have a regex which matches the string with NO whitespace(s) at the beginning. But the string containing whitespace(s) in the middle CAN match. So far i have tried below
[^-\s][a-zA-Z0-9-_\\s]+$
Debuggex Demo
Above is not allowing whitespace(s) at the beginning, but also not allowing in the middle/end of the string. Please help me.
In your 2nd character class, \\s will match \ and s, and not \s. Thus it doesn't matches a whitespace. You should use just \s there. Also, move the hyphen towards the end, else it will create unintentional range in character class:
^[^-\s][a-zA-Z0-9_\s-]+$
If you plan to match a string of any length (even an empty string) that matches your pattern and does not start with a whitespace, use (?!\s) right after ^:
/^(?!\s)[a-zA-Z0-9_\s-]*$/
^^^^^^
Or, bearing in mind that [A-Za-z0-9_] in JS regex is equal to \w:
/^(?!\s)[\w\s-]*$/
The (?!\s) is a negative lookahead that matches a location in string that is not immediately followed with a whitespace (matched with the \s pattern).
If you want to add more "forbidden" chars at the string start (it looks like you also disallow -) keep using the [\s-] character class in the lookahead:
/^(?![\s-])[\w\s-]*$/
To match at least 1 character, replace * with +:
/^(?![\s-])[\w\s-]+$/
See the regex demo. JS demo:
console.log(/^(?![\s-])[\w\s-]+$/.test("abc def 123 ___ -loc- "));
console.log(/^(?![\s-])[\w\s-]+$/.test(" abc def 123 ___ -loc- "));
You need to use this regex:
^[^-\s][\w\s-]+$
Use start anchor ^
No need to double escape \s
Also important is to use hyphen as the first OR last character in the character class.
\w is same as [a-zA-Z0-9_]
use \S at the beginning
^\S+[a-zA-Z0-9-_\\s]+$
This RegEx will allow neither white-space at the beginning nor at the end of. Your string/word and allows all the special characters.
^[^\s].+[^\s]$
This Regex also works Fine
^[^\s]+(\s+[^\s]+)*$
try this should work
[a-zA-Z0-9_]+.*$
/^[^.\s]/
try this instead it will not allow a user to enter character at first place
^ matches position just before the first character of the string
. matches a single character. Does not matter what character it is, except newline
\s is space
If your field for user name only accept letters and middle of space but not for begining and end
User name: /^[^\s][a-zA-Z\s]+[^\s]$/
If your field for user ID only accept letters,numbers and underscore and no spaces allow
user ID: /^[\w]+$/
If your field for password only accept letters,number and special character no spaces allow
Password: /^[\w##&]+$/
Note: \w content a-zA-Z, number, underscore (_) if you add more character, add you special character after \w.
You can compare with user ID and password field in password field im only add some special character (##&).
India public thoko like 😁
I suggest below regex for this,
^[^\s].*[^\s]$
You can try regex in here
Its given: /(\S)\1(\1)+/g matches all occurrences of three equal non-whitespace characters following each other.
I don't understand why there is () around (\S) and 2nd (\1), but not around 1st (\1). Can anyone help in explaining how above regex works?
src: http://www.javascriptkit.com/javatutors/redev2.shtml
Thnx in advance.
The \S needs parentheses to capture its value, so you can refer back to the captured value with \1. \1 means "match the same text which capturing group #1 matched".
I believe there is a problem with this regex. You said you want to match "three equal non-whitespace characters". But the + will make this match 3 or more equal, consecutive non-whitespace characters.
The g on the end means "apply this regex over the entire input string, or globally".
The second set of parentheses is not necessary. It needlessly captures the repeated character a second time, while matching the same strings as this regex:
/(\S)\1\1+/g
Also, as #AlexD pointed out, the description should say that it matches at least three characters. If you replaced that regex with BONK in the string fooxxxxxxbar:
'fooxxxxxxbar'.replace(/(\S)\1\1+/g, 'BONK')
..you might expect the result to be fooBONKBONKbar from their description, because there are two sets of three 'x's. But in fact the result would be fooBONKbar; the first \1 matches the second 'x', and the \1+ matches the third 'x' and any 'x's that follow it. If they wanted to match just three characters, they should have left the + off.
I noticed several other sloppy descriptions like that, plus at least one outright error: \B is equivalent to (?!\b) (a position that's not a word boundary), not [^\b] (a character that's not a backspace). For that matter, their description of word boundaries--"the position between a word and a space"--is wrong, too. A word boundary isn't defined by any particular character, like a space--in fact, it can just as well be the absence of any character that creates one. The string:
Word
...starts with a word boundary because 'W' is a word character and, being first, it's not preceded by another word character. Similarly, the 'd' is not followed by another word character, so the end of the string is also a word boundary.
Also, a regex doesn't know from words, only word characters. The definition of a word character can vary depending on the regex flavor and Unicode or locale settings, but it always includes [A-Za-z0-9_] (ASCII letters and digits plus the underscore). A word boundary is simply a position that's between one of those characters and any other character (or no other character, as I explained earlier).
If you want to learn about regexes, I suggest you forget that site and start here instead: regular-expressions.info.