JS regular expression for repeated unique character - javascript

I am trying to determine whether a string contains exactly one given distinct character, repeated at least once. For example "bbbbb" should match but "ccMMcc" should not as it contains two different letters, c and m respectively. Presuming regular expressions would be the simplest way to do so, shame I suck at them, what will I need to match my string against?

You can use a backreference:
^(.)\1+$

The regex is:
\b(\w)\1*\b
that is:
\b: word boundary
(\w): the first char, group
\1*: any number of repetition of the first char

"tttt".match(/^(.)\1*$/) returns ["tttt","t"] but "test".match(/^(.)\1*$/) returns null

Related

Regular expression that matches a single word in any order

After a long, unsuccessful search I am starting to wonder if what I am looking for is possible, I would like a regular expression which requires that each letter chosen is mandatory but only once and in any order.
Example : ^[abc]{3}$
The result I expect would be that it matches only that :
abc, bac, cba, acb
While I get :
acc, abb, cca, aab
Do you see where I am going with this?
You may use a regex like this with a negative lookahead of the matched character in a back-reference:
^(?:([abc])(?!.*\1)){3}$
RegEx Demo
Here is another way.
^(?!.*([abc]).*\1)[abc]{3}$
Demo
The negative lookahead
(?!.*([abc]).*\1)
asserts that no character is repeated and
[abc]{3}
together with the two anchors asserts that the string has a length of three and is composed of the characters in the character class.

Regular expression to include numeric only or character only or ignore first two conditions if alpha numeric

I wrote Regular expression for the below cases :
only numbers(length:4)
only alphabets(should contain vowel)
([0-9]{1,4})|((?=[a-z]*[aeiou])[a-z]*)
eg: 9987, tyde
How to add the below condition?
Ignore the first two cases if the string contains alphanumeric
characters.
eg: 9ty87
If I decypher well your question, I think your are looking for that:
a string with only digits and between one and four characters
a string with only letters with at least a vowel
a string with only letters and digits with at least one letter and one digit.
pattern:
/^(?:[0-9]{1,4}|[bcdfghj-np-tv-z]*[aeiou][a-z]*|[a-z]+[0-9][a-z0-9]*|[0-9]+[a-z][a-z0-9]*)$/i
or more factorized
/^(?:[0-9]{1,4}(?:[0-9]*[a-z][a-z0-9]*)?|[bcdfghj-np-tv-z]*(?:[aeiou][a-z]*|[a-z]+[0-9][a-z0-9]*))$/i
It is a simple alternation (I don't think you need something more complicated). So only one of the branches will succeed.
Note that anchors ^ and $ are essential for this kind of task to ensure that whole string is taken in account.

Regexp: numbers and few special characters

I am buried in a RegExp hell and can't find way out, please help me.
I need RegExp that matches only numbers (at least 1 number) and one of this characters: <, >, = (exactly one of them one time).
My reg. expression looks like this:
^[0-9]+$|^[=<>]{1}$
And I thought it should match when my string containts one or more digits and exactly 1 special character defined by me. But it doesn't act correctly. I think there might be problem with my start/end of string definition but Im not sure about that.
Examples that should pass include:
<1
=2
22>
>1
=00123456789
Examples that should not pass this reg. exp.:
<<2
==222
<>=2
I thought it should match when my string containts one or more digits and exactly 1 special character
No, the original pattern matches a string contains one or more digits or exactly 1 special character. For example it will match 123 and = but not 123=.
Try this pattern:
^\d+[=<>]$
This will match that consists of one or more digits, followed by exactly one special character. For example, this will match 123= but not 123 or =.
If you want your special character to appear before the number, use a pattern like this instead:
^[=<>]\d+$
This will match =123 but not 123 or =.
Update
Given the examples you provided, it looks like you want to match any string which contains one or more digits and exactly one special character either at the beginning or the end. In that case use this pattern:
^([=<>]\d+|\d+[=<>])$
This will match <1, =2, 22>, and >1, but not 123 or =.
Just use [0-9]+[=<>]
Here are visualizers of your regexp and this one:
http://www.regexper.com/#%5E%5B0-9%5D%2B%24%7C%5E%5B%3D%3C%3E%5D%7B1%7D%24
http://www.regexper.com/#%5B0-9%5D%2B%5B%3D%3C%3E%5D
Your regex says:
1 or more numbers OR 1 symbol
Also, the ^ and $ means the whole string, not contains. if you want a contains, drop them. I don't know if you have a space between the number and symbol, so put in a conditional space:
[0-9]+\s?[=<>]{1}
This should work.
^[0-9]+[=<>]$
1 or more digits followed by "=<>".
Try this regex:
^\d+[=<>]$
Description
This one:
/^\d+[<>=]$|^[<>=]\d+$/

How to make this javascript regex greedy?

I am having trouble making zero or one '?' give preference to one occurrence over zero, in javascript.
Ex. This is my regex: (=1)?
This is my string, str: abcd=1
when i do regex.exec(str) I get the following returned: ["",undefined]. It looks like it's choosing the zero length match in the beginning of my string. Is there a way to get it to choose '=1'? Possibly this may work differently in other languages but I'm currently using javascript, and this seems to be the case.
The expression (=1)? will give precedence to one occurrence over zero, but the regular expression engine will always attempt to match as early in the string as possible. So starting at the first character in the string, first it will try to match =1 and fail, and then because of the ? it will match the empty string.
I think the following expression is most similar to what your intention is:
(?:.*(=1))?
This will put =1 into the first capture group if it is anywhere in the string, but every string will still be matched because of the ? making the non-capturing group optional.
By defaut ? is greedy in javascript. Your problem is somewhere else.
aside note: to have ? lazy you must write ?? (like other quantifiers)
(=1)* will match =1=1=1=1=1=1=1=1
If you're looking to match all digits after the = then use
abcde=(\d+)
This will place all the digits into capture group 1.

Regex to count the number of capturing groups in a regex

I need a regex that examines arbitrary regex (as a string), returning the number of capturing groups. So far I have...
arbitrary_regex.toString().match(/\((|[^?].*?)\)/g).length
Which works for some cases, where the assumption that any group that starts with a question mark, is non-capturing. It also counts empty groups.
It does not work for brackets included in character classes, or escaped brackets, and possibly some other scenarios.
Modify your regex so that it will match an empty string, then match an empty string and see how many groups it returns:
var num_groups = (new RegExp(regex.toString() + '|')).exec('').length - 1;
Example: http://jsfiddle.net/EEn6G/
The accepted answer is what you should use in any production system. However, if you wanted to solve it using a regex for fun, you can do that as shown below. It assumes the regex you want the number of groups in is correct.
Note that the number of groups is just the number of non-literal (s in the regex. The strategy we're going to take is instead of matching all the correct (, we're going to split on all the incorrect stuff in between them.
re.toString().split(/(\(\?|\\\[|\[(?:\\\]|.)*?\]|\\\(|[^(])+/g).length - 1
You can see how it works on www.debuggex.com.

Categories

Resources