Regex to match '-' delimited alphanumeric words - javascript

I would like to test if user type only alphanumeric value or one "-".
hello-world -> Match
hello-first-world -> match
this-is-my-super-world -> match
hello--world -> NO MATCH
hello-world-------this-is -> NO MATCH
-hello-world -> NO MATCH (leading dash)
hello-world- -> NO MATCH (trailing dash)
Here is what I have so far, but I dont know how to implement the "-" sign to test it if it is only once without repeating.
var regExp = /^[A-Za-z0-9-]+$/;

Try this:
/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/
This will only match sequences of one or more sequences of alphanumeric characters separated by a single -. If you do not want to allow single words (e.g. just hello), replace the * multiplier with + to allow only one or more repetitions of the last group.

Here you go (this works).
var regExp = /^[A-Za-z0-9]+([-]{1}[A-Za-z0-9]+)+$/;
letters and numbers greedy, single dash, repeat this combination, end with letters and numbers.

(^-)|-{2,}|[^a-zA-Z-]|(-$) looks for invalid characters, so zero matches to that pattern would satisfy your requirement.

I'm not entirely sure if this works because I haven't done regex in awhile, but it sounds like you need the following:
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)+$/
You're requirement is split up in the following:
One or more alphanumeric characters to start (that way you ALWAYS have an alphanumeric starting.
The second half entails a "-" followed by one or more alphanumeric characters (but this is optional, so the entire thing is required 0 or more times). That way you'll have 0 or more instances of the dash followed by 1+ alphanumeric.
I'm just not sure if I did the regex properly to follow that format.

The expression can be simplified to: /^[^\W_]+(?:-[^\W_]+)+$/
Explanation:
^ match the start of string
[^\W_]+ match one or more word(a-zA-Z0-9) chars
(?:-[^\W_]+)+ match one or more group of '-' follwed by word chars
$ match the end of string
Test: https://regex101.com/r/MODQxw/1

Related

Javascript in regexp not matching something

I want to match everything except the one with the string '1AB' in it. How do I do that? When I tried it, it said nothing is matched.
var text = "match1ABmatch match2ABmatch match3ABmatch";
var matches = text.match(/match(?!1AB)match/g);
console.log(matches[0]+"..."+matches[1]);
Lookarounds do not consume the text, i.e. the regex index does not move when their patterns are matched. See Lookarounds Stand their Ground for more details. You still must match the text with a consuming pattern, here, the digits.
Add \w+ word matching pattern after the lookahead. NOTE: You may also use \S+ if there can be any one or more non-whitespace chars. If there can be any chars, use .+ (to match 1 or more chars other than line break chars) or [^]+ (matches even line breaks).
var text = "match100match match200match match300match";
var matches = text.match(/match(?!100(?!\d))\w+match/g);
console.log(matches);
Pattern details
match - a literal substring
(?!100(?!\d)) - a negative lookahead that fails the match if, immediately to the right of the current location, there is 100 substring not followed with a digit (if you want to fail the matches where the number starts with 100, remove the (?!\d) lookahead)
\w+ - 1 or more word chars (letters, digits or _)
match - a literal substring
See the regex demo online.

How to make regex for 3 slashes?

I want to write regex for following
students/ad34567-06c1-498c-9b15-cdbac695c1f2/data/sessions
Where students, data and sessions should be exact match.
i have tried this
[students]\[a-z]\[a-z]\[a-z]
You can try this regex, although your question is not clear to me.
^students\/([\w\-\d]+)\/data\/sessions$
Check here https://regex101.com/r/xnxwCX/1
you can grab the data in between students/, /data/session.
In your regex [students]\\[a-z]\\[a-z]\\[a-z] you are trying to match with word students in a character class [students] which will match one of the specified characters instead of matching the whole word.
To match a forward slash you have to use \/ instead of //. [a-z] is specified without a quantifier and will match 1 character from a-z.
To match your example string you might use
^students\/[a-z0-9]+(?:-[a-z0-9]+)+\/data\/sessions$
Regex demo
This part [a-z0-9]+(?:-[a-z0-9]+)+ matches one or more times a lowercase character or a digit [a-z0-9]+
Following a non capturing group repeated one or more times that will match a hyphen followed by matching one or more times a lowercase character or a digit (?:-[a-z0-9]+)+
You might also use [a-f0-9] if your characters are a -f

Regex to allow numbers and digits but only one comma

I have a task where the user should be able to edit the first line of an address field but they should only be able to use one comma but can put that one comma anywhere in the string.
I was wondering if there was a way that this could be done in JavaScript?
so far I have tried:
^[a-zA-Z0-9\&\-\,\.\/\'_ ]+$
But this regex allows me to enter multiple commas.
So I want the regex to allow the user to do this:
21, Tash Place N13 2IJ
or this:
,Tash Place 21 N13 2IJ
But not this:
21, Tash Place, N13, 2IJ
Any help would be appreciated
You may use
/^[-a-zA-Z0-9&.\/'_ ]*(?:,[-a-zA-Z0-9&.\/'_ ]*)?$/
See the regex demo.
Here,
^ - matches the start of string,
[-a-zA-Z0-9&.\/'_ ]* - matches 0+ letters, digits or -./'_ symbols, then
(?:,[-a-zA-Z0-9&.\/'_ ]*)? - an optional sequence (1 or 0 occurrences) of:
, - a comma (thus, only one is allowed)
[-a-zA-Z0-9&.\/'_ ]* - matches 0+ letters, digits or -./'_ symbols, then
$ - end of string.
Another way is to add a (?!(?:[^,]*,){2}) negative lookahead to your regex:
/^(?!(?:[^,]*,){2})[-a-zA-Z0-9&.\/',_ ]+$/
^^^^^^^^^^^^^^^^^
See another regex demo
The (?!(?:[^,]*,){2}) lookahead will fail the match if there are 2 sequences of 0+ chars other than , and then a , in the string.

Simple regex with repeated unordered matches

I have this regex
/^[a-z]{1,}( (?=[a-z])){0,}(_(?=[a-z])){0,}[a-z]{0,}$/
I want to match
ag_b_cf_ajk
or
zva b c de
or
hh_b opxop a_b
so any character tokens separated by a single space or underscore.
(In the regex above, we have a literal space, which is legal, and we have look-aheads that ensure that a space or underscore is followed by a character).
The problem is, my above regex is only matching the first space or underscore, like so:
axz_be
axz be
but these fail
axz_be_j
axz be j
I believe I missing some concept with regexes in order to solve this as I have been trying for the last few hours!
It seems you can just use
^[a-z]+(?:[_ ][a-z]+)*$
See the regex demo
The regex matches
^ - start of string
[a-z]+ - one or more lowercase ASCII letters
(?:[_ ][a-z]+)* - zero or more sequences of:
[_ ] - a space or an underscore
[a-z]+ - one or more lowercase ASCII letters
$ - end of string
If the space or underscore must appear at least once, use the + quantifier instead of *:
^[a-z]+(?:[_ ][a-z]+)+$
^
To add a multicharacter alternative to the underscore and hyphen, you need to introduce another non-capturing group:
^[a-z]+(?:(?:[_ ]|\[])[a-z]+)+$
See another regex demo

RegExp extract specific string followed by any number with leading / trailing whitespace

I want to extract a string from another using JavaScript / RegExp.
Here is what I got:
var string = "wp-button wp-image-45 wp-label";
string.match(/(?:(?:.*)?\s+)?(wp-image-([0-9]+))(:?\s(?:.*)?)?/);
// returnes: ["wp-button ", "wp-image-45", "45", undefined]
I just want to have "wp-image-45", so:
(Optional) any character
(Optional) followed by whitespace
(Required) followed by "wp-image-"
(Required) followed by any number
(Optional) followed by whitespacy
(Optional) followed by any character
What is missing here? Is it just some kind of bracketing or more?
I also tried
string.match(/(?:(?:.*)?\s+)?(?=(wp-image-([0-9]+)))(?=(:?\s(?:.*)?)?)/)
Edit: In the end I just want to have the number. But I'd also make this step in between.
Regexps are not required to start matching at the beginning of the string, so your attempts to match whitespace and any character aren't necessary. Also, "any character" includes whitespace (except newlines in certain modes).
This should be all you need:
string.match(/\bwp-image-(\d+)\b/)
This will capture, for example, "wp-image-123" into matching group 0, and "123" into matching group 1.
\b means "word boundary", which ensures that you won't match "abcwp-image-123def". A word boundary is defined as any place where a non-word character is followed by a word character, or vice versa. A word character is consists of a letter, a number or an underscore.
Also, I used \d instead of [0-9] simply out of convenience. They have slightly different meaning (\d also matches characters considered numbers in other languages), but that won't make a difference in your case.
If all of that surrounding stuff is optional and all you want is the number then there's no point to matching for any of that stuff except for that "wp-image-" prefix, just do:
var string = "wp-button wp-image-45 wp-label";
string.match(/wp-image-([0-9]+)/);

Categories