I have a requirement to find and return the first occurrence of the pattern from a string.
Example: Please find my model number RT21M6211SR/SS and save it
Expected output: RT21M6211SR/SS
Condition for the pattern to match
Combination of digits and alphabets
Character length between 6 to 14
May or may not contain special characters like '-' or '/'
Starts with always alphabet
What I tried, but it didn't work for 4th condition
var str = 'Please find my model number RT21M6211SR/SS and save it';
var reg = /\b(\w|\d)[\d|\w-\/]{6,14}\b/;
var extractedMNO = '';
var mg = str.match(reg) || [""];
console.log('regular match mno', mg[0]);
\w matches word characters, which includes _ and digits as well. If you only want to match alphabetical characters, use [a-z] to match the first character.
Also, because you want to match lengths of 6-14, after matching the first character, you should repeat the character set with {5,13}, so that the repeated characters plus the first character comes out to a length of 6-14 characters.
var str = 'Please find my model number RT21M6211SR/SS and save it';
console.log(str.match(/\b[a-z][a-z0-9\/-]{5,13}/gi)[2]);
But since the matched string must contain digits (and doesn't just permit digits), then you need to make sure a digit exists in the matched substring as well, which you can accomplish by using lookahead for a digit right after matching the alphabetical at the start:
var str = 'Please find my model number RT21M6211SR/SS and save it';
console.log(str.match(/\b[a-z](?=[a-z\/-]{0,12}[0-9])[a-z0-9\/-]{5,13}/gi));
// ^^^^^^^^^^^^^^^^^^^^^^^
If you want to permit other special characters, just add them to the character set(s).
Related
I am attempting to create a regex pattern that matches a line where all the words begin with uppercase letters, regardless of length. It must also account for any number of equals signs ('=') being on either side.
For example matches:
==This Would Match==
===I Like My Cats===
====Number Of Equals Signs Does Not Matter===
=====Nor Does Line Length Etc.=====
Non-matches:
==This would not regardless of its length==
===Nor would this match, etc===
How do I write this pattern?
You could match one or more equals signs at either side like =+.
To match words that begin with a capital letter could start with [A-Z] followed by \w one or more times. If you want to match more characters than \w, you could create a character class [\w.] to add matching a dot for example.
This pattern would match between equals sign(s) zero or more times a word that starts with an uppercase character followed by a whitespace, and ends with a word that starts with an uppercase character:
^=+(?:[A-Z]\w* )*(?:[A-Z][\w.]+)=+$
const strings = [
"==This Would Match==",
"===I Like My Cats===",
"====Number Of Equals Signs Does Not Matter===",
"=====Nor Does Line Length Etc.=====",
"==This would not regardless of its length==",
"===Nor would this match, etc===",
"=aaaa="
];
let pattern = /^=+(?:[A-Z]\w* )*(?:[A-Z][\w.]+)=+$/;
strings.forEach((s) => {
console.log(s + " ==> " + pattern.test(s));
});
This matches your desired results:
var test = [
"==This Would Match==",
"===I Like My Cats===",
"====Number Of Equals Signs Does Not Matter===",
"=====Nor Does Line Length Etc.=====",
"==This would not regardless of its length==",
"===Nor would this match, etc==="
]
var reg = /=*([A-Z]\w*\W*)+=*/g;
console.log(test.map(t => t.match(reg) == t));
Try this regex:
^=*[A-Z][^ ]*( [A-Z][^ ]*)*=*$
It allows for any number (including 0) of = signs on either side and requires every word to start with a capital letter.
The * quantifier means 0 or more times.
[^ ] is a negated character class, meaning it matches anything except a space.
You can try it online here.
I want to accept words and some special characters, so if my regex
does not fully match, let's say I display an error,
var re = /^[[:alnum:]\-_.&\s]+$/;
var string = 'this contains invalid chars like ##';
var valid = string.test(re);
but now I want to "filter" a phrase removing all characters not matching the regex ?
usualy one use replace, but how to list all characters not matching the regex ?
var validString = string.filter(re); // something similar to this
how do I do this ?
regards
Wiktor Stribiżew solution works fine :
regex=/[^a-zA-Z\-_.&\s]+/g;
let s='some bloody-test #rfdsfds';
s = s.replace(/[^\w\s.&-]+/g, '');
console.log(s);
Rajesh solution :
regex=/^[a-zA-Z\-_.&\s]+$/;
let s='some -test #rfdsfds';
s=s.split(' ').filter(x=> regex.test(x));
console.log(s);
JS regex engine does not support POSIX character classes like [:alnum:]. You may use [A-Za-z0-9] instead, but only to match ASCII letters and digits.
Your current regex matches the whole string that contains allowed chars, and it cannot be used to return the chars that are not matched with [^a-zA-Z0-9_.&\s-].
You may remove the unwanted chars with
var s = 'this contains invalid chars like ##';
var res = s.replace(/[^\w\s.&-]+/g, '');
var notallowedchars = s.match(/[^\w\s.&-]+/g);
console.log(res);
console.log(notallowedchars);
The /[^\w\s.&-]+/g pattern matches multiple occurrences (due to /g) of any one or more (due to +) chars other than word chars (digits, letters, _, matched with \w), whitespace (\s), ., & and -.
To match all characters that is not alphanumeric, or one of -_.& move ^ inside group []
var str = 'asd.=!_#$%^&*()564';
console.log(
str.match(/[^a-z0-9\-_.&\s]/gi),
str.replace(/[^a-z0-9\-_.&\s]/gi, '')
);
I'm trying to write code that removes all after the first block of numbers and text.Do you have any idea how to do this.
string = '009EPMT18$MBS'
the expected result
string = '009EPMT'
You'll need regex to do that. It's a string analysis syntax common in many languages. There are many regular expressions which would do what you want, here's one:
var myRegex = /^[0-9]+[a-zA-Z]+/;
^ means that the search must begin at the start of the string.
[0-9] means that right after the beginning, there must be characters in the 0 to 9 range.
+ means there must be one or more of the previous condition, meaning there must be one or more digits.
[a-zA-Z] means there must be any character in the range a to z or A to Z. This won't include accented characters and letters from other alphabets though.
Calling .exec(string) on a regex returns an array of found strings in the passed string.
You were on the right track, the letters were just missing from your pattern:
var s = '009EPMT18$MBS';
var result;
var m = s.match(/^\d+[A-Z]+/); // first numbers and uppercase text
if (m) result = m[0]; // result = "009EPMT"
Regex explanation: beginning of string ^ followed by 1 or more digits \d+ followed by 1 or more letters from A to Z [A-Z]+. Note that lowercase characters will not match.
I am trying to do a basic string replace using a regex expression, but the answers I have found do not seem to help - they are directly answering each persons unique requirement with little or no explanation.
I am using str = str.replace(/[^a-z0-9+]/g, ''); at the moment. But what I would like to do is allow all alphanumeric characters (a-z and 0-9) and also the '-' character.
Could you please answer this and explain how you concatenate expressions.
This should work :
str = str.replace(/[^a-z0-9-]/g, '');
Everything between the indicates what your are looking for
/ is here to delimit your pattern so you have one to start and one to end
[] indicates the pattern your are looking for on one specific character
^ indicates that you want every character NOT corresponding to what follows
a-z matches any character between 'a' and 'z' included
0-9 matches any digit between '0' and '9' included (meaning any digit)
- the '-' character
g at the end is a special parameter saying that you do not want you regex to stop on the first character matching your pattern but to continue on the whole string
Then your expression is delimited by / before and after.
So here you say "every character not being a letter, a digit or a '-' will be removed from the string".
Just change + to -:
str = str.replace(/[^a-z0-9-]/g, "");
You can read it as:
[^ ]: match NOT from the set
[^a-z0-9-]: match if not a-z, 0-9 or -
/ /g: do global match
More information:
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
Your character class (the part in the square brackets) is saying that you want to match anything except 0-9 and a-z and +. You aren't explicit about how many a-z or 0-9 you want to match, but I assume the + means you want to replace strings of at least one alphanumeric character. It should read instead:
str = str.replace(/[^-a-z0-9]+/g, "");
Also, if you need to match upper-case letters along with lower case, you should use:
str = str.replace(/[^-a-zA-Z0-9]+/g, "");
str = str.replace(/\W/g, "");
This will be a shorter form
We can use /[a-zA-Z]/g to select small letter and caps letter sting in the word or sentence and replace.
var str = 'MM-DD-yyyy'
var modifiedStr = str.replace(/[a-zA-Z]/g, '_')
console.log(modifiedStr)
Hi i have a field in php that will be validated in javascript using i.e for emails
var emailRegex = /^[\w-\.]+#([\w-]+\.)+[\w-]{2,4}$/;
What i'm after is a validation check which will look for the
first letter as a capital Q
then the next letters can be numbers only
then followed by a .
then two numbers only
and then an optional letter
i.e Q100.11 or Q100.11a
I must admit i look at the above email validation check and i have no clue how it works but it does ;)
many thanks for any help on this
Steve
The ^ marks the beginning of the string, $ matches the end of the string. In other words, the whole string should exactly match this regular expression.
[\w-\.]+: I think you wanted to match letters, digits, dots and - only. In that case, the - should be escaped (\-): [\w\-\.]+. The plus-sign makes is match one or more times.
#: a literal # match
([\w-]+\.)+ letters, digits and - are allowed one or more times, with a dot after it (between the parentheses). This may occur several times (at least once).
[\w-]{2,4}: this should match the TLD, like com, net or org. Because a TLD can only contain letters, it should be replaced by [a-z]{2,4}. This means: lowercase letters may occur two till four times. Note that the TLD can be longer than 4 characters.
An regular expression which should follow the next rules:
a capital Q (Q)
followed by one or more occurrences of digits (\d+)
a literal dot (.)
two digits (\d{2})
one optional letter ([a-z]?)
Result:
var regex = /Q\d+\.\d{2}[a-z]?/;
If you need to match strings case-insensitive, add the i (case-insensitive) modifier:
var regex = /Q\d+\.\d{2}[a-z]?/i;
Validating a string using a regexp can be done in several ways, one of them:
if (regex.test(str)) {
// success
} else {
// no match
}
var emailRegex = /^Q\d+\.\d{2}[a-zA-Z]?#([\w-]+\.)+[a-zA-Z]+$/;
var str = "Q100.11#test.com";
alert(emailRegex.test(str));
var regex = /^Q[0-9]+\.[0-9]{2}[a-z]?$/;
+ means one or more
the period must be escaped - \.
[0-9]{2} means 2 digits, same as \d{2}
[a-z]? means 0 or 1 letter
You can check your regex at http://regexpal.com/