Hi I need help in detecting words that are nonsensical like these: Okkkk or alrrriiigghht
I found this block of code:
var string = "alrrright";
var regex = /(\w)\1+/g;
var res = regex.test(string);
alert(res);
It returns false if it detects that there are no characters that repeats more than once and true if there are any. I need to raise the number of repeated characters. How do I do that? Sorry I really suck at regex.
Replace the + with {n-1,}, where n is the number of repeated characters:
var regex = /(\w)\1{2,}/g;
Debuggex Demo
Related
I've got this text: 3,142 people. I need to remove the people from it and get only the number, also removing comma(s). I need it to work with any higher numbers too like 13,142 or even 130,142 (at every 3 digits it will get a new comma).
So, in short, I need to get the numeric characters only, without commas and people. Ex: 3,142 people -> 3142.
My first version that didn't work was:
var str2 = "3,142 people";
var patt2 = /\d+/g;
var result2 = str2.match(patt2);
But after I changed patt2 to /\d+[,]\d+/g, it worked.
you can use this:
var test = '3,142 people';
test.replace(/[^0-9.]/g, "");
It will remove every thing except digit and decimal point
'3,142 people'.replace(/[^\d]/g, ''); // 3142
JSFiddle Demo: http://jsfiddle.net/zjx2hn1f/1/
Explanation
[] // match any character in this set
[^] // match anything NOT in character set
\d // match only digit
[^\d] // match any character that is NOT a digit
string.replace(/[^\d]/g, '') // replace any character that is NOT a digit with an empty string, in other words, remove it.
If I search a string for matches to a regex which is the union of two or more sub-regexen, is there any way to determine which sub-regex matches without checking each of them individually?
For example, if I have the code:
var regExp = new RegExp('ab|cd');
var matches = regExp.allMatches('absolutely fabulous');
the search returns two matches - but is there a way for me to know which match corresponds to which sub-regex?
Found an answer thanks to searching for branches.
var regExp = new RegExp('(ab)|(cd)'); //brackets are significant
var matches = regExp.allMatches('absolutely fabulous');
var m1 = match.first;
print(m1.group(1)); // 'ab'
print(m1.group(2)); // null, since second term (cd) not matched here
var m2 = match.last;
print(m2.group(1)); // null, since first pattern not matched here
print(m2.group(2)): // 'ac'
Other useful info at
dart regex matching and get some information from it
I want to extract a portion of a string, allowing for the dash character to appear randomly throughout. In my match, I want the dash character occurrences to be included.
Let's say I have a scenario like so:
haystack = "arandomse-que-nce"
needle = "sequence"
and I want to come out on the other end with a string like se-que-nce this this case, what would the regex pattern look like?
I would split the string and then join by -*; for example, in JavaScript:
var needle = "sequence"
var regex = new RegExp(needle.split('').join('-*'))
var result = "arandomse-que-nce".match(regex) // ["se-que-nce"]
var result2 = "a-bad-sequ_ence".match(regex) // null
You could also use a regex to insert -* between each character:
var regex = new RegExp(needle.replace(/(?!$|^)/g, '-*'))
Both the split/join method and the replace method return 's-*e-*q-*u-*e-*n-*c-*e' for the regex.
If you have characters like * in your string, that have meanings in regular expressions, you may want to escape them, like so:
var regex = new RegExp(needle.replace(/(?!$|^)/g, '-*')
.replace(/([-\\^$*+?.()|[\]{}])/g, '\\$1'))
Then, if needle was 1+1, for example, it would give you 1-*\+-*1 for the regex.
s-*e-*q-*u-*e-*n-*c-*e-*
The assumes that multiple hyphens in a row are okay.
EDIT: Doorknob's split/join solution is good, but be aware that it only works for character that aren't special characters (*, +, etc.)
I don't know what the specifications are, but if there are special characters, make sure to escape them:
new RegExp(needle.split('').map(function(c) { return '\\' + c; }).join('-*'))
You could try to use:
s-?e-?q-?u-?e-?n-?c-?e
My field is supposed to be in the format of A111-1A1, but my regex allows the very last number to be more than one digit (eg. A111-1A1212341). How do I fix this?
Below is the regex I am currently using.
var validchar = /^[A-Z](([0-9]{3})+\-)[0-9][A-Z][0-9]+$/;
Remove the + at the end of your pattern. That is what allows for more than one numeric at the end.
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
However, your pattern otherwise doesn't look right to do what you say you want. Is that really the exact pattern you are using?
Try this
var validchar = /^[A-Z][0-9]{3}\-[0-9][A-Z][0-9]$/;
Or remove the + from the end of your regex
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
Just remove the final + from your regex:
var validchar = /^[A-Z]([0-9]{3})+\-[0-9][A-Z][0-9]$/;
Hi I have a some text where I want to find all occurrences like the following and replace with that same number minus the apostrophes.
'1' or '164'
(pattern = apostrophe number apostrophe)
Reg Ex makes my brain sore.
Any help much appreciated.
'(16[0-4]|1[0-5][0-9]|[1-9][0-9]?)'
matches a number between 1 and 164, enclosed in apostrophes. To remove the apostrophes, replace the matched text with backreference \1.
If I understood correctly, this can help you (example using Javascript):
var x = "some_text:'68' and other:'109', finally:'05'";
var res = x.replace(/'([0-9]+)'/g,"$1");
alert(res); //some_text:68 and other:109, finally:05