URL last part regular expression - javascript

I need to get the last part of my URL with html on the end. So if I have this url http://step/build/index.html I need to get only index.html. I need to do this with javascript
let address = http://step/build/index.html;
let result = address.match(/html/i);
I tried this, but it doesn't work for me, maybe I make some mistakes.
How do I get the last segment of URL using regular expressions
Could someone help me and explain it in details?
Thank you.

You can extract the .html filename part using this /[^/]+\.html/i RegEx.
See the code below.
const regex = /[^/]+\.html/i;
let address = "http://step/build/index.html";
let result = address.match(regex);
console.log(result);
The same RegEx will also match the filename incase the URL has additional parameters.
const regex = /[^/]+\.html/i;
let address = "http://step/build/index.html?name=value";
let result = address.match(regex);
console.log(result);

You could split it on slashes and then fetch the last item:
let address = "http://step/build/index.html";
let result = address.split("/").pop();
console.log(result)

Here's a non-regex approach. Should be more reliable/appropriate at the job, depending on whether you'll need other URL-specific parts:
// Note the ?foo=bar part, that URL.pathname will ignore below
let address = 'http://step/build/index.html?foo=bar';
let url = new URL(address);
// Last part of the path
console.log(url.pathname.split('/').pop());
// Query string
console.log(url.search);
// Whole data
console.log(url);

You could use split which returns an array to split on a forward slash and then use pop which removes the last element from the array and returns that:
let address = "http://step/build/index.html".split('/').pop();
console.log(address);
If you have querystring parameters which could for example start with ? or #, you might use split again and get the first item from the array:
let address2 = "\"http://step/build/index.html?id=1&cat=2"
.split('/')
.pop()
.split(/[?#]/)[0];
console.log(address2);

Related

Temporarily remove URL from string

I've created a Twitter bot that copies the tweet of a certain user and then uwu-fies them, meaning it just changes some characters to make them funny, Elon becomes Ewon for example. Now of course it's very debatable how funny this actually is but I think that's besides the point for now.
If I got a tweet with a URL, of course the URL can't be uwu-fied since it would become invalid. The way I've sold this right now is, search for the URL using a regex, replace it with a performance.now() (I used to use a UUID v4 but that also contains characters that would get uwu-fied) and save an object with the URL and performance.now() that was used.
Then when the uwu-fication is done I can reconstruct is using the saved object, this does work but it feels like a bodged solution. The only other solution I could think of is generating a UUID that only contains characters that won't get uwu-fied?
EDIT:
Based of the current marked answer I've solved the problem by transforming my code into this:
// Split the sentence into words
const words = sentence.split(` `);
const pattern = new RegExp(/(?:https?|ftp):\/\/[\n\S]+/g);
// If the word is a URL just attach it to the new string without uwufying
let uwufied = ``;
words.forEach(word => uwufied += ` ${pattern.test(word) ? word : uwufyWord(word)}`);
You can split the tweet into an array .split(" "), and then run over that array with a foreach loop. You can handle the tweet word by word then. At the start of your handle process you would check that the "word" is not an url. Then handle your replacements.
let tweet = "Hello World. What's up?"
let arr = tweet.split(" ")
let output = ""
for (word of arr) {
// Check that it's not an URL here
// Replace here
output += word + " "
}
// Use output here
console.log(output)

Need to extract part of a url using Regex [duplicate]

This question already has answers here:
Template literal inside of the RegEx
(2 answers)
Closed 2 years ago.
I would like to extract a substring from an s3 URL using Regex rather than with string manipulation functions.
My requirement is to retrieve dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19 out of a URL s3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19
However, I have not been able to arrange the regex expression to give me what I want.
I would like the regex to parse in this form but I know that I am missing something in the regex line.
const url = 's3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19';
const patternMatches = url.match(new RegExp(s3://${s3bucket}/${dynamodbtablename}/([a-f\d-]+)));
const migrationDataFileS3Key = patternMatches[indexOfResultingArrayWithDesiredSubstring]
I was able to come up with the expression below to retrieve the UUID/GUID and have had to concatenate it with ${s3bucket} to form the S3 bucket key. However, I am not happy with this solution. I require the above.
const url = 's3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19';
const patternMatches = url.match(/([a-f\d-]+)/g);
const migrationDataFileS3Key = massiveTableItem + '/' + patternMatches[patternMatches.length - 1];
Thank you very much for your help.
You may not need a regular expression: split the URL on / and take the element you need from that. Like:
{
console.log(`s3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19`
.split(`/`) // split on forward slash
.slice(-2) // take the last 2 elements from the resulting array
.join(`/`) // extract it
);
// alternatively
console.log(`s3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19`
.match(/([\w\-])+/g)
.slice(-2)
.join(`/`)
);
// or (use capture groups)
const {groups: {root, hashpath}} =
/(?<root>s3:\/\/s3bucket\/)(?<hashpath>[\w\-\/]+)/
.exec(`s3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19`);
console.log(hashpath);
// or (just substring from known index)
const url = `s3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19`;
console.log(url.substr(url.indexOf(`/`, 5) + 1))
}
you can use capture groups, like
var str = "s3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19";
var myRegexp = /s3:\/\/s3bucket\/(.*)/;
var match = myRegexp.exec(str);
console.log(match[1]);
// returns 'dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19'
I was able to eventually arrive at a solution that was closest to the format that I wanted as required in my question. I was able to do it by combining the solution of #sudhir-bastakoti and #wiktor-stribiżew as each individual answer did not address my question completely.
I am grateful to everyone that answered my question including #kooiinc. I checked out his last answer options and it worked. However, I wanted the answer in a certain format.
const s3bucket = 's3bucket';
const url = 's3://s3bucket/dynamodbtablename/05abd315-2e0b-4717-919d-1cc6576ebe19';
const migrationDataFileS3Key = url.match(new RegExp(String.raw`s3://${s3bucket}/(.*)`))[1];

Regex - Match a string between second occurance of characters

I have a string of text that looks something like this:
?q=search&something=that&this=example/
In that example, I need to grab that . I'm using the following regex below:
var re = new RegExp("\&(.*?)\&");
Which going re[1] is giving me:
something=that - but it needs to be only that
I tried:
var re = new RegExp("\=(.*?)\&");
But that gives me everything from the first equals sign, so:
search&something=that
Is the output when it just needs to be:
that
I need to somehow target the second occurrences of 2 characters and grab whats in between them. How best do I go about this?
You can use
/something=([^&]+)/
and take the first group, see the JavaScript example:
let url = '?q=search&something=that&this=example/';
let regex = /something=([^&]+)/
let match = regex.exec(url);
console.log(match[1]);
split seems more suited to your case:
"?q=search&something=that&this=example/".split("&")[1].split("=")[1]
Then you could also implement a simple method to extract any wanted value :
function getValue(query, index) {
const obj = query.split("&")[index];
if(obj) obj.split("=")[1]
}
getValue("?q=search&something=that&this=example/", 1);

Get the file name from Css propery background url

$('.icon-displayer').css('background-image');
console.log(a)
gives me value as url("http://localhost:8080/myApp/icons/xing-square.png")
from this string i want extract only the file name i.e xing-suare.png how do i do it?
I tried
var url= $('.icon-displayer').css('background-image');
var filename = url.split('/').pop()
did not work
Javascript pop method remove the last element from an array. Use split and then get the last position of the array.
var url= $('.balaIconPicker-icon-displayer').css('background-image');
var array = url.split('/');
var filename = array[array.length - 1];
If there are no parameters, then Lucas's answer is okay and it's the one you should use. However if the string at the end is something like "test.php?id=125" you will get the "?id=125" too which may not be what you want. A regular expression can save you from this:
var url = "http://www.test.com/directory/test.php?id=128",
cleanRegexp = /\/([^\.\/]+\.[a-z]{0,3})[^\/]*$/;
var result = cleanRegexp.exec(url);
window.alert(result[1]);
the regular expression finds the last slash, then looks after it for anything that isn't a slash or a dot, then grabs the dot and the extension, finishing before any special characters.
Here is the Fiddle

Regex to find id in url

I have the following URL:
http://example.com/product/1/something/another-thing
Although it can also be:
http://test.example.com/product/1/something/another-thing
or
http://completelydifferentdomain.tdl/product/1/something/another-thing
And I want to get the number 1 (id) from the URL using Javascript.
The only thing that would always be the same is /product. But I have some other pages where there is also /product in the url just not at the start of the path.
What would the regex look like?
Use window.location.pathname to
retrieve the current path (excluding
TLD).
Use the JavaScript string
match method.
Use the regex /^\/product\/(\d+)/ to find a path which starts with /product/, then one or more digits (add i right at the end to support case insensitivity).
Come up with something like this:
var res = window.location.pathname.match(/^\/product\/(\d+)/);
if (res.length == 2) {
// use res[1] to get the id.
}
/\/product\/(\d+)/ and obtain $1.
Just, as an alternative, to do this without Regex (though i admit regex is awfully nice here)
var url = "http://test.example.com//mypage/1/test/test//test";
var newurl = url.replace("http://","").split("/");
for(i=0;i<newurl.length;i++) {
if(newurl[i] == "") {
newurl.splice(i,1); //this for loop takes care of situatiosn where there may be a // or /// instead of a /
}
}
alert(newurl[2]); //returns 1
I would like to suggest another option.
.match(/\/(\d+)+[\/]?/g)
This would return all matches of id's present.
Example:
var url = 'http://localhost:4000/#/trees/8/detail/3';
// with slashes
var ids = url.match(/\/(\d+)+[\/]?/g);
console.log(ids);
//without slashes
ids = url.match(/\/(\d+)+[\/]?/g).map(id => id.replace(/\//g, ''));
console.log(ids);
This way, your URL doesn't even matter, it justs retrieves all parts that are number only.
To just get the first result you could remove the g modifier:
.match(/\/(\d+)+[\/]?/)
var url = 'http://localhost:4000/#/trees/8';
var id = url.match(/\/(\d+)+[\/]?/);
//With and without slashes
console.log(id);
The id without slashes would be in the second element because this is the first group found in the full match.
Hope this helps people.
Cheers!

Categories