If I have an E.164 formatted phone number and I want to look up any users with that phone number, though their number may not be formatted, what would that regex look like?
Example:
Given:
+1234567891
Regex should match any of:
(123) 456 7891
123-456-7891
+1234567891
123.456.7891
1234567891
Any of the above with trailing or leading whitespace.
var str = '+1234567891',
parts = str.match(/\+((\d{3})(\d{3})(\d{4}))/).slice(1),
num = parts.shift(),
rg = new RegExp(
'\\s*(?:\\+?' + [
num,
parts.join('.'),
parts.join('-'),
'\\(' + parts[0] + '\\) ' + parts.slice(1).join(' ')
].join('|') + ')\\s*');
In this case, it will produce
/\s*(?:\+?1234567891|123.456.7891|123-456-7891|\(123\) 456 7891)\s*/
Try:
/\s*(\+)?(\(\d{3}\)|\d{3})([ -.]|)\d{3}(\3)\d{4}\s*/
See the regex101 example
How it works
\s* matches any preceding white space
(\+)? optionally matches +
(\(\d{3}\)|\d{3}) matches (123) or just 123
([ -.]|) matches , -, . or nothing
\d{3} matches 123
(\3) whatever was matches by ( |-|.|)
\d{4} match 1234
\s* matches any following white space
I think the best you can do is normalize both numbers and compare the result. You can, for example, remove everything that is not a number ([^0-9]) and compare only the digits left.
You can use following regex :
^\s?(\(\d{3}\)|\+?\d{3})([\s-.])?\d{3}\2\d{4}\s?$
Demo :https://www.regex101.com/r/jM2fF4/6
Related
I am trying to remove all the characters from the string after comma except the first letter. The string is basically the last name,first name.
For example:
Smith,John
I tried as below but it removes comma and everything after comma.
let str = "Smith,John";
str = str.replace(/\s/g, ""); // to remove all whitespace if there is any at the beginning, in the middle and at the end
str = str.split(',')[0];
Expected output: Smith,J
Thank you!
Or try (,\w).* with replace:
let str = "Smith,John";
str = str.replace(/(,\w).*/, '$1');
console.log(str);
Try this regex out:
\w+,\w
This matches one or more characters before the comma and then matches only 1 character.
Here is the demo: https://regex101.com/r/bKpWt7/1
Note: \w matches any character from [a-zA-Z0-9_].
Taking optional spaces around the comma in to account, and perhaps multiple "names" before the comma:
*([^\s,][^,\n]*?) *, *([^\s,]).*
* Match optional spaces
( Capture group 1
*([^\s,] Match optional spaces and match at least a single char other than a whitespace char or a ,
[^,\n]*? Match any char except a , or a newline non greedy
) Close group 1
*, * Match a comma between optional spaces
([^\s,]) Capture group 2, match a single char other than , or a whitespace char
.* Match the rest of the line
Regex demo
In the replacement using group 1 and group 2 with a comma in between $1,$2
const regex = / *([^\s,][^,\n]*?) *, *([^\s,]).*/;
[
"Smith,John Jack",
"Smith Lastname , Jack John",
"Smith , John",
" ,Jack"
].forEach(s => console.log(s.replace(regex, "$1,$2")));
I need to call minus/hyphen (-) as minus when it comes between one number or any other character.
otherwise, if it comes between characters I need to replace it with an empty string.
For replacing '-' to empty string
readOutText = 'akhila-hegde'
readOutText = readOutText.replace(/([A-Za-z])-([A-Za-z]){0}\w/gi, ' '); // replace '-' with ' '
Now replace '-' with 'minus' in all of the scenarios
9 - {{response}}
9-{{response}}
{{response}}-9
{{response}} - 9
7346788-literallyanything
literallyanything-347583475
I am trying expression in this in this site https://regexr.com/
I have tried a couple of RegEX for 9-{{response}} tyeps.
readOutText = readOutText.replace(/([0-9])[ ]{0,1}-[ ]*\w/gi, ' minus '); // Not working
readOutText = readOutText.replace(/([0-9])[ ]{0,1}-([a-zA-Z0-9!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/? ])\w/gi, ' minus ')
Both of these are matching with 9-{ in the string 9-{{response}}
If I know the above type I can do the same for {{response}}-473 type
Can someone help me with what I have missed?
I made a small example using capture groups to capture the hyphen and classify as minus or true hpyhen. https://regex101.com/r/zb2eZv/1
Capture Group 1 is minus and capture group 2 is hyphen.
(?:\d+(-)\d+)|(?:\w+(-)\w+)
https://jex.im/regulex/#!flags=&re=(%3F%3A%5Cd%2B(-)%5Cd%2B)%7C(%3F%3A%5Cw%2B(-)%5Cw%2B)
It's a litte resource-intensive, and there might be cleverer solutions, but I believe you need a multipass replace.
Replace in case of digit dash digit
Replace in case of digit dash not-digit
Replace in case of not-digit dash digit
This will ignore all non-digit dash non-digit cases.
By the way, [0-9] can be replaced with \d (digit) and [ ]{0,1} can be replaced with * (zero or more spaces).
const inputs = [
"akhila-hegde",
"6 -4",
"9 - {{response}}",
"9-{{response}}",
"{{response}}-9",
"{{response}} - 9",
"7346788-literallyanything",
"literallyanything-347583475",
"7- 8 and some-composed - word then 4-9"
];
const transformMinus = word => word
.replace(/(\d) *- *(\d)/g, "$1 minus $2")
.replace(/(\d) *- *(\D)/g, "$1 minus $2")
.replace(/(\D) *- *(\d)/g, "$1 minus $2")
const outputs = inputs.map(transformMinus);
console.log(outputs);
I have this regex that should match when there's two numbers in brackets
/(P|C\(\d+\,{0,1}\s*\d+\))/g
for example:
C(1, 2) or P(2 3) //expected to match
C(43) or C(43, ) // expect not to match
but it also matches the ones with only 1 number, how can i fix it?
You have a couple of issues. Firstly, your regex will match either P on its own or C followed by numbers in parentheses; you should replace P|C with [PC] (you could use (?:P|C) but [PC] is more performant, see this Q&A). Secondly, since your regex makes both the , and spaces optional, it can match 43 without an additional number (the 4 matches the first \d+ and the 3 the second \d+). You need to force the string to either include a , or at least one space between the numbers. You can do that with this regex:
[PC]\(\d+[ ,]\s*\d+\)
Demo on regex101
Try this regex
[PC]\(\d+(?:,| +) *\d+\)
Click for Demo
Explanation:
[PC]\( - matches either P( or C(
\d+ - matches 1+ digits
(?:,| +) - matches either a , or 1+ spaces
*\d+ - matches 0+ spaces followed by 1+ digits
\) - matches )
You can relax the separator between the numbers by allowing any combination of command and space by using \d[,\s]+\d. Test case:
const regex = /[PC]\(\d+[,\s]+\d+\)/g;
[
'C(1, 2) or P(2 3)',
'C(43) or C(43, )'
].forEach(str => {
let m = str.match(regex);
console.log(str + ' ==> ' + JSON.stringify(m));
});
Output:
C(1, 2) or P(2 3) ==> ["C(1, 2)","P(2 3)"]
C(43) or C(43, ) ==> null
Your regex should require the presence of at least one delimiting character between the numbers.
I suppose you want to get the numbers out of it separately, like in an array of numbers:
let tests = [
"C(1, 2)",
"P(2 3)",
"C(43)",
"C(43, )"
];
for (let test of tests) {
console.log(
test.match(/[PC]\((\d+)[,\s]+(\d+)\)/)?.slice(1)?.map(Number)
);
}
I want to have a regular expression in JavaScript which help me to validate a string with contains only lower case character and and this character -.
I use this expression:
var regex = /^[a-z][-\s\.]$/
It doesn't work. Any idea?
Just use
/^[a-z-]+$/
Explanation
^ : Match from beginning string.
[a-z-] : Match all character between a-z and -.
[] : Only characters within brackets are allowed.
a-z : Match all character between a-z. Eg: p,s,t.
- : Match only strip (-) character.
+ : The shorthand of {1,}. It's means match 1 or more.
$: Match until the end of the string.
Example
const regex= /^[a-z-]+$/
console.log(regex.test("abc")) // true
console.log(regex.test("aBcD")) // false
console.log(regex.test("a-c")) // true
Try this:
var regex = /^[-a-z]+$/;
var regex = /^[-a-z]+$/;
var strs = [
"a",
"aB",
"abcd",
"abcde-",
"-",
"-----",
"a-b-c",
"a-D-c",
" "
];
strs.forEach(str=>console.log(str, regex.test(str)));
Try this
/^[a-z-]*$/
it should match the letters a-z or - as many times as possible.
What you regex does is trying to match a-z one single time, followed by any of -, whitespace or dot one single time. Then expect the string to end.
Use this regular expression:
let regex = /^[a-z\-]+$/;
Then:
regex.test("abcd") // true
regex.test("ab-d") // true
regex.test("ab3d") // false
regex.test("") // false
PS: If you want to allow empty string "" to pass, use /^[a-z\-]*$/. Theres an * instead of + at the end. See Regex Cheat Sheet: https://www.rexegg.com/regex-quickstart.html
I hope this helps
var str = 'asdadWW--asd';
console.log(str.match(/[a-z]|\-/g));
This will work:
var regex = /^[a-z|\-|\s]+$/ //For this regex make use of the 'or' | operator
str = 'test- ';
str.match(regex); //["test- ", index: 0, input: "test- ", groups: undefined]
str = 'testT- ' // string now contains an Uppercase Letter so it shouldn't match anymore
str.match(regex) //null
I need a regular expression that will match any character that is not a letter or a number. Once found I want to replace it with a blank space.
To match anything other than letter or number you could try this:
[^a-zA-Z0-9]
And to replace:
var str = 'dfj,dsf7lfsd .sdklfj';
str = str.replace(/[^A-Za-z0-9]/g, ' ');
This regular expression matches anything that isn't a letter, digit, or an underscore (_) character.
\W
For example in JavaScript:
"(,,#,£,() asdf 345345".replace(/\W/g, ' '); // Output: " asdf 345345"
You are looking for:
var yourVar = '1324567890abc§$)%';
yourVar = yourVar.replace(/[^a-zA-Z0-9]/g, ' ');
This replaces all non-alphanumeric characters with a space.
The "g" on the end replaces all occurrences.
Instead of specifying a-z (lowercase) and A-Z (uppercase) you can also use the in-case-sensitive option: /[^a-z0-9]/gi.
This is way way too late, but since there is no accepted answer I'd like to provide what I think is the simplest one: \D - matches all non digit characters.
var x = "123 235-25%";
x.replace(/\D/g, '');
Results in x: "12323525"
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Match letters only /[A-Z]/ig
Match anything not letters /[^A-Z]/ig
Match number only /[0-9]/g or /\d+/g
Match anything not number /[^0-9]/g or /\D+/g
Match anything not number or letter /[^A-Z0-9]/ig
There are other possible patterns
try doing str.replace(/[^\w]/);
It will replace all the non-alphabets and numbers from your string!
Edit 1: str.replace(/[^\w]/g, ' ')
Just for others to see:
someString.replaceAll("([^\\p{L}\\p{N}])", " ");
will remove any non-letter and non-number unicode characters.
Source
To match anything other than letter or number or letter with diacritics like é you could try this:
[^\wÀ-úÀ-ÿ]
And to replace:
var str = 'dfj,dsf7é#lfsd .sdklfàj1';
str = str.replace(/[^\wÀ-úÀ-ÿ]/g, '_');
Inspired by the top post with support for diacritics
source
Have you tried str = str.replace(/\W|_/g,''); it will return a string without any character and you can specify if any especial character after the pipe bar | to catch them as well.
var str = "1324567890abc§$)% John Doe #$#'.replace(/\W|_/g, ''); it will return str = 1324567890abcJohnDoe
or look for digits and letters and replace them for empty string (""):
var str = "1324567890abc§$)% John Doe #$#".replace(/\w|_/g, ''); it will return str = '§$)% #$#';
Working with unicode, best for me:
text.replace(/[^\p{L}\p{N}]+/gu, ' ');