I need a little assistants with my poor regex chops. I am trying to grab the 41398 from the URL. This number is a variable
http://www.website.com/beta/flips/index/41398_1363874140/friend/756
I figured out how to grab everything up to the / after index.
var url = document.URL;
var myRegexp = /index(.*)/;
var match = myRegexp.exec(url);
this leaves me with /41398_1363874140/friend/756....
Is there a solution with regEx to grab the number between / and _ ?
I tried
var url = document.URL;
var myRegexp = \/(.*?)\_;
var match = myRegexp.exec(url);
and of course the '/' is the issue.
If you know there will always be an underscore you can use:
var url = document.URL;
var myRegexp = /\d+_/;
var match = myRegexp.exec(url);
Do you need the first match? If not, then you can directly use:
var url = document.URL;
var myRegexp = /index\/(.*?)_/;
var match = myRegexp.exec(url);
Well, you were almost close. Underscores do not need to be escaped.
Otherwise, you could use your first match (adding those after your first 3 lines of code above):
var myNewRegexp = /\/(.*?)_/;
var NewMatch = myNewRegexp.exec(match);
You can use:
/\/index\/(\d+)/
which means the first group matches the number.
Here is a regex that matches just the number (?<=index/)[0-9]*(?=_). You didn't say what language you are working in so that would help make this more specific
Related
Kapil Arora <kapil.arora#abc.in>
How to find the name before angular bracket
This is the RegEx I used ([^<]+). but it is not finding first String
I would just add a start of input anchor ^ to the head of your expression, plus a look ahead for a space so you get just the name (no trailing space):
^[^<]+(?= )
No need for brackets; group 0 is the whole match, which is what you want.
See live demo.
Since you haven't specified any language. I would be solving in JavaScript.
Lets assume an email id as "kapil.sharma123#gmail.com".
Thus the program would be something like this:
var email = "kapil.sharma123#gmail.com";
var regex = /(^[A-Za-z]+\.+[A-Za-z]+)/;
var res = email.match(regex)[1];
res = res.split(".").join(" ");
Here I am matching the regex with the email id string and then extracting from the first index.
Finally I am spliting on "." and joining with a blankspace.
Note : It also works for simple email ids like "kapil.sharma#gmail.com"
You may try this:
const regex = /^\s*([^<]+)\s*</g;
const str = `Kapil Arora <kapil.arora#abc.in> bla bla bla <asadfasdf>`;
var match = regex.exec(str);
console.log(match[1].trim());
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
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
I'm trying to find the words between the brackets.
var str = "asdfasdfkjh {{word1}} asdf fff fffff {{word2}} asdfasdf";
var pattern = /{{\w*}}/g;
var str.match(pattern); // ["{{word1}}","{{word2}}"]
This closes the deal, but gives it with the brackets, and i don't want them.
Sure, if I used the native replace on the results i could remove them. But i want the regexp to do the same.
I've also tried:
var pattern = /(?:{{)(\w*)(?:}})/g
but i can't find the real deal. Could you help me?
Edit: i might need to add a note that the words are dynamic
solution:
Bases on Tim Piezcker awnser i came with this solution:
var arr = [],
re = /{{(\w?)}}/g,item;
while (item = re.exec(s))
arr.push(item[1]);
In most regex flavors, you could use lookaround assertions:
(?<={{)\w*(?=}})
Unfortunately, JavaScript doesn't support lookbehind assertions, so you can't use them.
But the regex you proposed can be used by accessing the first capturing group:
var pattern = /{{(\w*)}}/g;
var match = pattern.exec(subject);
if (match != null) {
result = match[1];
}
A quick and dirty solution would be /[^{]+(?=\}\})/, but it will cause a bit of a mess if the leading braces are omitted, and will also match {word1}}. If I remember correctly, JavaScript does not support look-behind, which is a bit of a shame in this case.
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]$/;