Regex to find digits group after a text match [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have tried many regex expressions but unable to get the desired output.
My string is
Ms.Evalyn J Hobbs AND Mr.Jan K Hir sch AND Ms.Gale D Bannister 3611
I need to extract the digits group which come after Mr or Mrs that is 3611

Try Regex: (?:Ms|Mr|Mrs).*?(\d+) and get Group 1 value
Demo

Another way to capture digits in a group after Ms., Mr. Ms. (or maybe Miss.) could be:
\bM(?:rs?|s|iss)\.[^\d]+(\d+)
That would match
A word boundary \b to make sure it is not part of a larger match and then M
An alternation (?:rs?|s|iss)\. that would match one of the variants followed by a dot.
Match not a digit using a negated character class [^\d]+
At the end capture one or more digits in a capturing group (\d+)
const regex = /\bM(?:rs?|s|iss)\.[^\d]+(\d+)/g;
const str = "Ms.Evalyn 33 J Hobbs AND Mr.Jan K Hir 55 years Mr. sch AND Ms.Gale D Bannister 3611";
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[1]);
}

Related

How to remove digits from string. based on the length and remove other letters with it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
const item = "Dell Model 23506A Laptop M2";
const item2 = item.replace(/\d{2,}/g, "");
The above code will remove any words with "more than 2 digits found in a row, but will only remove the numbers from the Word.
Example the end result of the above will be "Dell Model A Laptop M2", leaving the A from 23506A.
How do you write the logic, that if more than 2 digits found in a row in a Word, to remove the entire word as a result.
Example the end result of the above should be "Dell Model Laptop M2"
Thus removing 23056A entirely, because more than 2 digits we're found in a row (right after another).
This should work:
item.replace(/[^\s]*\d{2,}[^\s]*/g, "");
You may also wish to get rid of the adjacent space:
item.replace(/[^\s]*\d{2,}[^\s]*\s?/g, "");
If you don't want to remove all other strings that could possibly contain 2 digits like for example 1,50, you can assert at least a char a-zA-Z
\b(?=\w*[A-Za-z])\w*\d{2}\w*\b
Explanation
\b A word boundary
(?=\w*[A-Za-z]) Positive lookahead, assert a char a-zA-Z
\w*\d{2}\w* Match 2 digits between optional word characters
\b A word boundary
Regex demo

Split without some spaces [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Good morning,
If I have a string:
"#Hello(first second), one two"
And I want an array:
["#Hello(first second)", "one", "two"]
How I can do this?
Thanks,
You can use .split() with RegExp /\s(?!\w+\))/ to match space character not followed by word characters followed by closing parenthesis ")"
var str = "#Hello(first second), one two";
var res = str.split(/\s(?!\w+\))/);
console.log(res);
Alternatively you can use .match() with RegExp /#\w+\(\w+\s\w+(?=\))\)|\w+(?=\s|$)/g to match "#" followed by one or more word characters followed by "(" followed by word characters followed by space character followed by word characters followed by closing parenthsis ")" or word characters followed by space or end of string
var str = "#Hello(first second), one two";
var res = str.match(/#\w+\(\w+\s\w+(?=\))\)|\w+(?=\s|$)/g);
console.log(res);

Regex first letter not integer with jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have textbox and I want get string value. But I want users to not be able to enter the string that has number on first letter. As matter of fact I want to replace number with '' null.
for example
1test =====convert=======> test
you can simply use ^[a-zA-Z]
^ starts with a-z or A-Z
or if you want special character too then use ^\D
^\D : Matches anything other than a decimal digit
Regex Demo
you can use $text.replace(/^[^0-9]+/, '')
/^ beginning of the line
[^0-9]+ match anything other than digits at-least once
thanks # Wiktor and Tushar
here is the solution: You can check on this live regex.
https://regex101.com/r/OJfyv4/1
$re = '/\b[a-z][a-z0-9]*/';
$str = '1test';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
This works your case:
^\d+
https://regex101.com/r/daezA9/1
^ asserts position at start of the string
\d matches a digit (equal to [0-9])

Regular Expression (Regex) on CSS3 Transform String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Given a string "translateX(-50%) scale(1.2)" with N transform functions
1) How can I match the names ["translateX", "scale"]?
2) How can I match the values ["-50%", "1.2"]?
If you absolutely must use regular expression to do this, you can use the exec() method in a loop, pushing the match result of the captured group(s) to the desired arrays of choice.
var str = 'translateX(-50%) scale(1.2)'
var re = /(\w+)\(([^)]*)\)/g,
names = [], vals = [];
while (m = re.exec(str)) {
names.push(m[1]), vals.push(m[2]);
}
console.log(names) //=> [ 'translateX', 'scale' ]
console.log(vals) //=> [ '-50%', '1.2' ]
The regular expression uses two capture groups, the first matches/captures word characters only, the second uses negation which will match any character except ) "zero or more" times.
Try something like (\w+)\((.+?)\):
(\w+): Match the regular expression below and capture its match into backreference number 1
\w+: Match a single character that is a “word character” (letters, digits, and underscores)
+: Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\(: Match the character “(” literally
(.+?): Match the regular expression below and capture its match into backreference number 2
.+?: Match any single character that is not a line break character
+?: Between one and unlimited times, as few times as possible, expanding as needed (lazy)
\): Match the character “)” literally
var str = "translateX(-50%) scale(1.2)",
regex = /(\w+)\((.+?)\)/g,
match, names = [], values = [];
while(match = regex.exec(str)) {
names.push(match[1]);
values.push(match[2]);
}

Regular Expression to check a number with first three characters are alphabets not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
$(document).ready(function(){
$("#mystring").keyup(function(){
var name= document.getElementById('mystring').value;
var re = ^[ABC]{3}\\d{14}$;
if(!re.test(mystring))
{
alert("mystringformat invalid");
}
else{
alert("mystringformat valid");
}
});
});
This is not a regex literal:
^[ABC]{3}\\d{14}$
In JavaScript, regex literals are surrounded in / characters.
Please read: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
regex definition is faulty, try:
var re = /^[ABC]{3}\d{14}$/;
Strange that you want to check a number whose first three characters must be alphabet.
If you mean input string then you can try this
var re = /^[ABC]{3}[\d+]{14}/;
Regular expression:
/^[a-zA-Z]{3}[0-9]{14}$/
Online example
Explanations:
[a-zA-Z]{3} match a single character present in the list below, exactly 3 times
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
[0-9]{14} match a single character present in the list below, exactly 14 times
0-9 a single character in the range between 0 and 9
Javascript:
$("#mystring").keyup(function () {
var $name = $(this).val();
if (/^[a-zA-Z]{3}[0-9]{14}/.test($name)) {
alert("String is valid");
} else {
alert("String is invalid");
}
});
Online example

Categories