Regex in javascript complex - javascript

string str contains somewhere within it http://www.example.com/ followed by 2 digits and 7 random characters (upper or lower case). One possibility is http://www.example.com/45kaFkeLd or http://www.example.com/64kAleoFr. So the only certain aspect is that it always starts with 2 digits.
I want to retrieve "64kAleoFr".
var url = str.match([regex here]);

The regex you’re looking for is /[0-9]{2}[a-zA-Z]{7}/.
var string = 'http://www.example.com/64kAleoFr',
match = (string.match(/[0-9]{2}[a-zA-Z]{7}/) || [''])[0];
console.log(match); // '64kAleoFr'
Note that on the second line, I use the good old .match() trick to make sure no TypeError is thrown when no match is found. Once this snippet has executed, match will either be the empty string ('') or the value you were after.

you could use
var url = str.match(/\d{2}.{7}$/)[0];
where:
\d{2} //two digits
.{7} //seven characters
$ //end of the string
if you don't know if it will be at the end you could use
var url = str.match(/\/\d{2}.{7}$/)[0].slice(1); //grab the "/" at the begining and slice it out

what about using split ?
alert("http://www.example.com/64kAleoFr".split("/")[3]);

var url = "http://www.example.com/",
re = new RegExp(url.replace(/\./g,"\\.") + "(\\d{2}[A-Za-z]{7})");
str = "This is a string with a url: http://www.example.com/45kaFkeLd in the middle.";
var code = str.match(re);
if (code != null) {
// we have a match
alert(code[1]); // "45kaFkeLd"
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
The url needs to be part of the regex if you want to avoid matching other strings of characters elsewhere in the input. The above assumes that the url should be configurable, so it constructs a regex from the url variable (noting that "." has special meaning in a regex so it needs to be escaped). The bit with the two numbers and seven letter is then in parentheses so it can be captured.
Demo: http://jsfiddle.net/nnnnnn/NzELc/

http://www\\.example\\.com/([0-9]{2}\\w{7}) this is your pattern. You'll get your 2 digits and 7 random characters in group 1.

If you notice your example strings, both strings have few digits and a random string after a slash (/) and if the pattern is fixed then i would rather suggest you to split your string with slash and get the last element of the array which was the result of the split function.
Here is how:
var string = "http://www.example.com/64kAleoFr"
ar = string.split("/");
ar[ar.length - 1];
Hope it helps

Related

Having hard time with jQuery and replace string value

Im currently developing a posting [like What's on your mind] feature where im using twemoji plugin for emojis.
For some security reasons, i have to convert the emoji into its alt code/image filename before it stores to the database.
And convert it back to image when its being displayed on the feeds.
In my case I use [emoji=filename.png]
for example i have this string:
var string = "[emoji=1f938.png] [emoji=1f938-200d-2642-fe0f.png] [emoji=26f9-fe0f.png]";
string.replace(/-fe0f.png/g, '.png')
.replace(/\[emoji=(.*?)\]/g,'<img src="https://example.net/images/$1">');
the snippet above is working fine, but the only problem is it removes All -fe0f.png in the filename which causes some broken image.
What I want to achive is to remove the -fe0f.png part only when the filename length is <= 14. or maybe if the file name is consist of something like this: (char)-fe0f.png , but if it has more than (char) like (char)-(char)-(char)-fe0f.png, it should still remain the same..
the result should be:
from
[emoji=1f938.png] [emoji=1f938-200d-2642-fe0f.png] [emoji=26f9-fe0f.png]
to
[emoji=1f938.png] [emoji=1f938-200d-2642-fe0f.png] [emoji=26f9.png]
UPDATE:
I just noticed now that there are filenames like this 30-fe0f-20e3.png
but it needs to remove -fe0f in the middle.
so instead of [emoji=30-fe0f-20e3.png],
i need to have [emoji=30-20e3.png]
The file name length limit is equal to fourteen. Thus, there should be "nine" characters before the "-fe0f"
[^=] means all characters except "="
<![^=])a means there must not "=" before the "a"
<![^=]{9})a means it must not has a "=" character during the nine characters before the letter "a".
(?<![^=]{9})-fe0f.png means it must not has a "=" character during the nine characters before the "-fe0f.png".
So your new code should be like the below:
var string = "[emoji=1f938.png] [emoji=1f938-200d-2642-fe0f.png] [emoji=26f9-fe0f.png]";
string.replace(/(?<![^=]{9})-fe0f.png/g, '.png')
.replace(/\[emoji=(.*?)\]/g,'<img src="https://example.net/images/$1">');
Replacing the data in the example string:
const regex = /(\[emoji=[^\s\]\[]{0,13})-fe0f(\.png)/g;
let string = "[emoji=1f938.png] [emoji=1f938-200d-2642-fe0f.png] [emoji=26f9-fe0f.png]";
string = string.replace(regex, '$1$2');
console.log(string);
You can do the replacement in one replace call with a match and a capture group, matching 0-13 characters after emoji=
\[emoji=([^\s\]\[]{0,13})-fe0f\.png]
The pattern matches:
\[emoji= Match [emoji=
( Capture group 1
[^\s\]\[]{0,13} Match 0-13 times a non whitespace char except for [ and ]
) Close group 1
-fe0f\.png] Match literally (note to escape the dot)
regex demo
const regex = /\[emoji=([^\s\]\[]{0,13})-fe0f\.png]/g;
let string = "[emoji=1f938.png] [emoji=1f938-200d-2642-fe0f.png] [emoji=26f9-fe0f.png]";
string = string.replace(regex, '<img src="https://example.net/images/$1.png">');
console.log(string);
This should do it if you are just trying to not replace for greater than 14 chars.
if (string.length > 14) {
// do your replace here
}
Now, not sure if you are suggesting that if there's more than one "-" that you don't want to replace either.

Javascript regex for hashtag with number only

I want to match these kind of hashtag pattern #1, #4321, #1000 and not:
#01 (with leading zero)
#1aa (has alphabetical char)
But special character like comma, period, colon after the number is fine, such as #1.. Think of it as hashtag at the end of the sentence or phrase. Basically treat these as whitespace.
Basically just # and a number.
My code below doesn't meet requirement because it takes leading zero and it has an ugly space at the end. Although I can always trim the result but doesn't feel it's the right way to do it
reg = new RegExp(/#[0-9]+ /g);
var result;
while((result = reg.exec("hyha #12 gfdg #01 aa #2e #1. #101")) !== null) {
alert("\"" + result + "\"");
}
http://jsfiddle.net/qhoc/d3TpJ/
That string there should just match #12, #1 and #101
Please help to suggest better RegEx string than I had. Thanks.
You could use a regex like:
#[1-9]\d*\b
Code example:
var re = /#[1-9]\d*\b/g;
var str = "#1 hyha #12 #0123 #5 gfdg #2e ";
var matches = str.match(re); // = ["#1", "#12", "#5"]
This should work
reg = /(#[1-9]\d*)(?: |\z)/g;
Notice the capturing group (...) for the hash and number, and the non capturing (?: ..) to match the number only if it is followed by a white space or end of string. Use this if you dont want to catch strings like #1 in #1.. Otherwise the other answer is better.
Then you have to get the captured group from the match iterating over something like this:
myString = 'hyha #12 gfdg #01 aa #2e #1. #101';
match = reg.exec(myString);
alert(match[1]);
EDIT
Whenever you are working with regexps, you should use some kind of tool. For desktop for instance you can use The regex coach and online you can try this regex101
For instance: http://regex101.com/r/zY0bQ8

Matching the last digit or digits after a forward slash

In a JavaScript function in my Rails app, I'm trying to get the id of a recipe. Looking inside the event object like this
e.delegateTarget.baseURI
produces the uri, the last number of which (after the forward slash) is the id I want.
http://myroute.com/users/2/recipes/6
Of course, that id could be much longer in the future, so I need to be able to get all the digits after the last forward slash.
I tried to get the last id this way
var uri = e.delegateTarget.baseURI
var re = /^(.*[\\\/\d])/;
var recipe_id = uri.match(re);
The matched slash is supposed to be the last one because .* matches greedily, and then I look for any digit. This is all wrong. I'm not very experienced with regular expressions. Can you help?
A very simple way to do this would be to use string.split()
var uri = "http://myroute.com/users/2/recipes/6",
splituri = uri.split('/');
recipe_id = splituri[splituri.length - 1] // access the last index
Edit:
Even easier with the .pop() method, which returns the popped value
like elclanrs said.
var uri = e.delegateTarget.baseURI,
recipe_id = uri.split('/').pop();
You should use the special character $, which means end of input:
re = /\d+$/; //matches one or more digits from the end of the input
Here is a good resource on JavaScript regular expressions.

RegEx - Get All Characters After Last Slash in URL

I'm working with a Google API that returns IDs in the below format, which I've saved as a string. How can I write a Regular Expression in javascript to trim the string to only the characters after the last slash in the URL.
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9'
Don't write a regex! This is trivial to do with string functions instead:
var final = id.substr(id.lastIndexOf('/') + 1);
It's even easier if you know that the final part will always be 16 characters:
var final = id.substr(-16);
A slightly different regex approach:
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
Breaking down this regex:
\/ match a slash
( start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
) end of the captured group
\/? allow one optional / at the end of the string
$ match to the end of the string
The [1] then retrieves the first captured group within the match
Working snippet:
var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9';
var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];
// display result
document.write(afterSlashChars);
Just in case someone else comes across this thread and is looking for a simple JS solution:
id.split('/').pop(-1)
this is easy to understand (?!.*/).+
let me explain:
first, lets match everything that has a slash at the end, ok?
that's the part we don't want
.*/ matches everything until the last slash
then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"
(?!.*) this is "Negative lookahead"
Now we can happily take whatever is next to what we don't want with this
.+
YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:
(?!.*\/).+
this regexp: [^\/]+$ - works like a champ:
var id = ".../base/nabb80191e23b7d9"
result = id.match(/[^\/]+$/)[0];
// results -> "nabb80191e23b7d9"
This should work:
last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9
Don't know JS, using others examples (and a guess) -
id = id.match(/[^\/]*$/); // [0] optional ?
Why not use replace?
"http://google.com/aaa".replace(/(.*\/)*/,"")
yields "aaa"

How to find regexp in a string and attach it to a variable

Let's say we have a string in JavaScript "This is a nice website - http://stackoverflow.com". I want to extract the URL along with the three preceding characters (space dash space) using RegExp and attach the extracted string to a variable.
var string = "This is a nice website - http://stackoverflow.com";
var reg = ""; //no idea how to write this regexp for extracting url and three preceding chars
// and after some magic I would get
var extracedString = " - http://www.stackoverflow.com";
Anyone? Thanks.
var extractedString = string.replace(/^.*(...http:.+)$/, "$1");
if (extractedString == string) {
alert("No match");
}
The dot . matches every character, so three dots match three arbitrary characters. The ^ and $ match start and end of the string.
Note, that this won't work for
more than one URL
HTTPS, mailto, FTP, SSH, ... (although you can simply expand it, like this: (https?|ftp|ssh))

Categories