I have requirement in javascript where the starting and ending of a word is a Character with in between numbers.
e.g. S652354536667U
I tried with pattern
(/[A-Z]\d+[A-Z]$/).test(S652354536667U) // returns true ok
(/[A-Z]\d+[A-Z]$/).test(S65235Y4536667U) // returns true needed false
but it is allowing characters in between like S65235Y4536667U is being accepted.
Any help is appreciated.
Thanks
You need to put a caret at the start of the regex, to indicate the first letter is at the start of the string, i.e.:
^[A-Z]\d+[A-Z]$
You are missing the ^. It should be:
^[A-Z]\d*[A-Z]$
You can use this site to quickly test your regex: https://regexr.com/
You left caret in the beginning.
["S652354536667U", "S65235Y4536667U"].forEach(item => {
console.log(/^[A-Z]\d+[A-Z]$/.test(item))
})
You need to put ^ at the start and $ at the end in the reg ex. Otherwise if the pattern match anywhere else in the input, it will return true
(/^[A-Z]\d+[A-Z]$/).test(S652354536667U) // returns true
(/^[A-Z]\d+[A-Z]$/).test(S65235Y4536667U) // returns true
Check the below image. It is without specifying the start position. So, from the character 'Y' matching the reg ex
Below is with the starting position
Related
I am trying to write a regular expression which returns a three digit integer only. Not less than three or more than three. However my regex below is also true for four digit numbers. Am i missing something?
var threeDigits = /\d{3}$/
console.log(threeDigits.test("12"))// false
console.log(threeDigits.test("123"))// true
console.log(threeDigits.test("1234"))// true yet this is four digits???
You have the ending anchor $, but not the starting anchor ^:
var threeDigits = /^\d{3}$/
Without the anchor, the match can start anywhere in the string, e.g.
"1234".match(/\d{3}$/g) // ["234"]
Use either one ^[0-9]{3}$ or ^\d{3}$ .
I'm trying to come up with a Regexp that detects whether a string in Javascript is trimmed or not. That means, it doesn't have whitespace at the beginning or end.
So far I have
^[^\s].*[^\s]$
This works if the string is long, but for short strings such as a, it will not work because the pattern wants a non-space, any character, then another non-space. That a is trimmed, but doesn't follow the pattern.
Help me find the correct regex.
Try this to make a second char optional:
^[^\s](.*[^\s])?$
One option is to take your existing regex (^[^\s].*[^\s]$) and add a separate part to it specifically to test for a single non-space character (^[^\s]$), combining the two parts with |:
/(^[^\s]$)|(^[^\s].*[^\s]$)/
But I find sometimes it is simpler to test for the opposite case and then invert the result with !. So in your case, have the regex test for a space at the beginning or end:
!/(^\s)|(\s$)/.test(someString)
Encapsulated in a function:
function isTrimmed(s) {
return !/(^\s)|(\s$)/.test(s)
}
console.log(isTrimmed(" a ")) // false
console.log(isTrimmed("a ")) // false
console.log(isTrimmed(" a")) // false
console.log(isTrimmed("a")) // true
console.log(isTrimmed("abc")) // true
console.log(isTrimmed("a b c")) // true
console.log(isTrimmed("")) // true
You can use the below regex :
^([\s]+[\w]*)?([\w]*[\s]+)?$
Might not be the nicest answer but Negative lookahead should work:
/^(?!(?:.*\s$)|(?:^\s.*)).*$/
(?=^\S)[\W\w]*\S$
(?=^\S) Lookahead for non-space character at the start of the string (matches without consuming the string; will start match from first character again)
[\W\w]* Match any number of characters
\S$ Match non-space character at the end of the string
So for a:
(?=^\S) Matches a at the start of the string (resets current position to the start of the string)
[\W\w]* Matches no characters, as it needs to match the next part
\S$ Matches a at the end of the string
I want to get value of string's like follow.
If string's word start with some character like ("This is world").match(/^wo/g); It should return false But, i want it's true.
Because string has one word which start from wo.I don't want to execute line or string start with wo.
Please any one know using Regex how to check whole line's words check start with XYZ.
NOTE : I already Know that Line splitting with white-space and than after check every word start with XYZ But I don't want to split any line.
Thank you
firstly .match does NOT return boolean
you want regexp.test
(/\bwo/g).test("This is world");
\b matches a zero-width word boundary - see docs
try this :
var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/g);
A capturing or non-capturing group will do this job.
alert(/(^|\s)wo/.test("This is world"));
alert(/(^|\s)wo/.test("This is bar;world"));
I am trying to match an input of the following format
[Number]-[Number] using regex in JavaScript.
For ex :
1-100 OK
200-300 OK
-0992 NOT OK
aa-76 NOT OK
1- NOT OK
I tried:
^\d+(-\d+)*$
But this does not work at all.
Any pointers?
The reason it doesn't work is that the * quantifier makes the (-\d+) optional, allowing the regex to simply match 222. Remove it and all will be well. If you don't need the parentheses, strip them:
^\d+-\d+$
What if you want your numbers to be between 0 and 100 as in title?
If you want to make sure that your numbers on each side are in the range from 1 to 100, without trailing zeros, you can use this instead of \d:
100|[1-9]\d|\d
Your regex would then become:
^(?:100|[1-9]\d|\d)-(?:100|[1-9]\d|\d)$
What if the left number must be lower than the right number?
The regexes above will accept 2222-1111 for the first, 99-12 for the second (for instance). If you want the right number to be greater than the lower one, you can capture each number with capturing parentheses:
^(\d+)-(\d+)$
or
^(100|[1-9]\d|\d)-(100|[1-9]\d|\d)$
Then, if there is a match, say
if(regexMatcher.group(2) > regexMatcher.group(1)) { ... success ...}
The regex you are looking for is /\d+-\d+/. If you don't require the whole line to match the regex, then there is no need for surrounding ^ and $. For instance:
/\d+-\d+/.test("a-100")
// Result: false
/\d+-\d+/.test("-100")
// Result: false
/\d+-\d+/.test("10-100")
// Result: true
I'm maintaining some old code when I reached a headscratcher. I am confused by this regex pattern: /^.*$/ (as supplied as an argument in textFieldValidation(this,'true',/^.*$/,'','').
I interpret this regex as:
/^=open pattern
.=match a single character of any value (Except EOL)
*=match 0 or more times
$=match end of the line
/=close pattern
So…I think this pattern matches everything, which means the function does nothing but waste processing cycles. Am I correct?
It matches a single line of text.
It will fail to match a multiline String, because ^ matches the begining of input, and $ matches the end of input. If there are any new line (\n) or caret return (\r) symbols in between - it fails.
For example, 'foo'.match(/^.*$/) returns foo.
But 'foo\nfoo'.match(/^.*$/) returns null.
^ "Starting at the beginning."
. "Matching anything..."
* "0 or more times"
$ "To the end of the line."
Yep, you're right on, that matches empty or something.
And a handy little cheat sheet.
The regexp checks that the string doesn't contain any \n or \r. Dots do not match new-lines.
Examples:
/^.*$/.test(""); // => true
/^.*$/.test("aoeu"); // => true
/^.*$/.test("aoeu\n"); // => false
/^.*$/.test("\n"); // => false
/^.*$/.test("aoeu\nfoo"); // => false
/^.*$/.test("\nfoo"); // => false
Yes, you are quite correct. This regex matches any string that not contains EOL (if dotall=false) or any string (if dotall=true)