I have been trying to format properly the phone number so that all country can use at least. I am trying to use this format to accept as many regions as possible
Here is my pattern
$pattern = '^\+[0-9]?()[0-9](\s|\S)(\d[0-9]{9})$^';
the pattern matches well these formats but once I add a country code with 3 digits and space, it
fails
+441213315000
+1 2323214316
+2923432432432
I would like to match this format
+225 0546568022
Use
^\+[0-9]{1,3} ?[0-9]{10}$
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
[0-9]{1,3} any character of: '0' to '9' (between 1
and 3 times (matching the most amount
possible))
--------------------------------------------------------------------------------
? ' ' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[0-9]{10} any character of: '0' to '9' (10 times)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
PHP:
preg_match('/^\+[0-9]{1,3} ?[0-9]{10}$/', $string)
JavaScript:
/^\+[0-9]{1,3} ?[0-9]{10}$/.test(string)
Unlike our friend. I tried to fix your own pattern:
^\+([0-9][0-9]?[0-9]?)(\ )?([0-9]{10})$
Note that i've removed the ^ from end of pattern because it represents start of a new line!
This guy is your friend: https://regex101.com/
Good luck
Related
I'm not overly familiar with Regex and am just beginning with Javascript.
I'm trying to make it so when the user types in the textarea they are forced to follow a certain pattern. The pattern is 'X' followed by 9 numbers. If first character is any letter other than 'X' then it won't type, and after they do type 'X' if it is any character other than a number then it won't type, or if it's a series of numbers longer than 9 digits long then it will also not type.
Right now with the code I've got it will look for a letter for the first character, although not specifically an 'X', and kind of does the opposite in that it waits until this pattern is met and then replaces the text with nothing.
<textarea id="scan-data" onkeypress="if(this.value.match(/^[A-Za-z][0-9]{9,9}$/gm)) this.value=this.value.replace(/^[A-Za-z][0-9]{9,9}$/gm,'')" onkeyup="if(this.value.match(/^[A-Za-z][0-9]{9,9}$/gm)) this.value=this.value.replace(/^[A-Za-z][0-9]{9,9}$/gm,'')"></textarea><br>
The only text allowed in the textarea should be X and then 9 digits, anything else should automatically be replaced with "" (ie. nothing)
Would appreciate any help anyone can offer!
Thanks
Use
x.value.replace(/^(X\d{0,9})?.*$\n?/gm, (x,y) => y !== undefined ? y + (y.length==10 ? '\n' : '') : '');
See how the expression works.
JavaScript code:
function myFunction(x){
x.value = x.value.replace(/^(X\d{0,9})?.*$\n?/gm, (x,y) => y !== undefined ? y + (y.length==10 ? '\n' : '') : '');
}
<textarea id="scan-data" onkeyup="myFunction(this)"></textarea><br>
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to $1 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
X 'X'
--------------------------------------------------------------------------------
\d{0,9} digits (0-9) (between 0 and 9 times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of $1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in $1)
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ the end of the line
--------------------------------------------------------------------------------
\n? an optional LF (line feed) symbol
I wanted to create a regex that checks for non ASCII characters and some special characters.
/^[\x00-\x7F]+$/
'[^{}$]*'
The conditions are working individually, but not when I combine them. Tried with
/^([\x00-\x7F]|[^{}$]*)$/ but failing.
Any help would be much appreciated. Thanks in advance.
^[\x00-\x7F]+$ - matches a string completely composed of ASCII characters.
[^{}$]* matches zero or more characters different from {, } and $.
So, the second rule matches any string. If that is your intent, just use the first regular expression.
If you want to match any ASCII only string, excluding {, } and $ use
^(?=[\x00-\x7F]+$)[^{}$]*$
See proof.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[\x00-\x7F]+ any character of: '\x00' to '\x7F' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[^{}$]* any character except: '{', '}', '$' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Looks like there is something wrong with the this [\x00-\x7F] part of your regex. I've tried the following and it works.
^([[:ascii:]]+$|[^{}$]*)$
Given two strings s1 and s2, I am trying to write a regex that will match a string that starts with s1 but does not end with s2
Example (s1=TEST, s2=BAD)
TEST-101 match
TEST-SOME-DESC match
TEST-101-BAD should not match
TEST-SOME-DESC-BAD should not match
Here is what I tried for this example but it does not work: /^TEST-.*((?!BAD))$/
Try this:
/^(?!.+BAD$)TEST-.*/
This matches the start, goes ahead and rejects anything ending in the bad string, then matches the desired pattern.
Here's a demo that passes all four of your tests (click "RUN TESTS" at the bottom to verify).
You can use startsWith and endsWith
let arr = [`TEST-101`, `TEST-SOME-DESC`,`TEST-101-BAD`,`TEST-SOME-DESC-BAD`]
arr.forEach(e=>{
console.log(e, e.startsWith('TEST') && !e.endsWith('BAD'))
})
Use in JavaScript:
/^TEST-(?!.*BAD$).*/
In Ruby:
/\ATEST-(?!.*BAD\z).*/
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ / \A the beginning of the string
--------------------------------------------------------------------------------
TEST- 'TEST-'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
BAD 'BAD'
--------------------------------------------------------------------------------
$ / \z the end of the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
I would like to get parts of a string separated by the type (number or character)
Simplified initial situation:
var content = 'foo123bar456';
Desired result:
result = ['foo', 123, 'bar', 456];
This is what I have so far to match the first "foo"
^(([a-z])|([0-9]))+
I thought this would match either characters [a-z]+ OR numbers [0-9]+ (which would match 'foo' in this case) but unfortunately it allows both (characters AND numbers) at the same time.
If it would match only characters with the same type I could just add "{1,}" to my regex in order to match all occurrences of the pattern and the world would be a bit better.
Correct regex will be:
([a-zA-Z]+|[0-9]+)
Explanation of the regex is:
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[a-zA-Z]+ any character of: 'a' to 'z', 'A' to 'Z'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
Use g modifier as well for global match
[a-zA-Z]+|[0-9]+/g
I am attempting to validate text input with the following requirements:
allowed characters & length /^\w{8,15}$/
must contain /[a-z]+/
must contain /[A-Z]+/
must contain /[0-9]+/
must not contain repeated characters (ie. aba=pass and aab=fail)
Each test would return true when used with .test().
With modest familiarity, I am able to write the first 4 tests, albeit individually. The 5th test is not working out, negated lookahead (which is what i believe i need to be using) is challenging.
Here are a few value/result examples:
re.test("Fail1");//returns false, too short
re.test("StringFailsRule1");//returns false, too long
re.test("Fail!");//returns false, invalid !
re.test("FAILRULE2");//returns false, missing [a-z]+
re.test("failrule3");//returns false, missing [A-Z]+
re.test("failRuleFour");//returns false, missing [0-9]+
re.test("failRule55");//returns false, repeat of "5"
re.test("TestValue1");//returns true
Finally, the ideal would be a single combined test used to enforce all requirements.
This uses negative and positive lookaheads zero-length assertions for your tests and the .{8,15} bit validates length.
^(?!.*(.)\1)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])\w{8,15}$
For your fifth rule I used a negative lookahead to make sure that a capture group of any character is never followed by itself.
Regexpal demo
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
\w{8,15} word characters (a-z, A-Z, 0-9, _)
(between 8 and 15 times (matching the most
amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string