Get substring after a word and before an filename extension - javascript

I have a URL in the following format:
https://res.cloudinary.com/xyzzz/image/upload/v1673615977/dealetePosts/hokhqmcmmkveqhxtr0nb.jpg
How to extract hokhqmcmmkveqhxtr0nb from this?
So extract contents between dealetePosts and .jpg
String position, followed by substring would work but is there an easier way?
This is what I have so far and works but is this the best way?
const publicID = dealPic.substring(
dealPic.indexOf("dealetePosts/") + 13,
dealPic.lastIndexOf(".jpg")
);

You can use something like a split and pop method to slash "/" & "." characters. Thats if you are always expecting the same type of url.
let url = "https://res.cloudinary.com/xyzzz/image/upload/v1673615977/dealetePosts/hokhqmcmmkveqhxtr0nb.jpg";
let key = url.split("/").pop().split(".")[0];
console.log(key);

I use the substring function and a regex to remove any extension (jpg, png, etc) and it'works even if dealetePosts changed to anyother name
const test = "https://res.cloudinary.com/xyzzz/image/upload/v1673615977/dealetePosts/hokhqmcmmkveqhxtr0nb.jpg"
function substr(str = ""){
const lastIndexSlash = str.lastIndexOf("/") + 1
return str.substring(lastIndexSlash, str.length).replace(/\.[^/.]+$/, "");
}
console.log(substr(test))

Another option that could be considered easier to read and understand is using regular expressions to match the text you want to extract. The following code will match the text between "dealetePosts/" and ".jpg" and return it as the first captured group:
const publicID = dealPic.match(/dealetePosts\/(.*)\.jpg/)[1];

Related

Replace characters of a string matched by regex

I am in a situation to find the domain name of all valid URLs among a HTML page, replace these domain names with another domain name, but within the domain name, I need to do a 2nd replacement. For example, say the url https://www.example.com/path/to/somewhere is among the HTML page, I need to eventually transfer it into something like www-example-com.another.domain/path/to/somewhere.
I can do the first match and replace with the following code:
const regex = new RegExp('(https?:\/\/([^:\/\n\"\'?]+))', 'g');
txt = txt.replace(regex, "$1.another.domain");
but I have no idea how to do the second match and replace to replace the . into -. I wonder if there is any efficient way to finish this task. I tried to do something like the following but it does not work:
const regex = new RegExp('(https?:\/\/([^:\/\n\"\'?]+))', 'g');
txt = txt.replace(regex, "$1".replace(/'.'/g, '-') + ".another.domain");
Ok - I think I know what you're looking for. I'll explain what it's doing.
You 2 capture groups: the one before and the one after the first /.
You're taking the first capture group, and converting the . to -
You're adding via string .another.domain and then you're appending the 2nd capture group on it afterward
const address1 = 'https://www.example.com/path/to/somewhere';
const newDomain = "another.domain"
const pattern = /(https?:\/\/[^:\/\n\"\'?]+)(\/.*)/;
const matches = pattern.exec(address1);
const converted = matches[1].replace(/\./g, "-") + `.${newDomain}${matches[2]}`;
console.log(converted);
You can use the function version of String.prototype.replace() to have some more control over the specific replacements.
For example...
const txt = 'URL is https://www.example.com/path/to/somewhere'
const newTxt = txt.replace(/(https?:\/\/)([\w.]+)/g, (_, scheme, domain) =>
`${scheme}${domain.replace(/\./g, '-')}.another.domain`)
console.log(newTxt)
Here, scheme is the first capture group (https?:\/\/) and domain is the second ([\w.]+).
If you need a fancier domain matcher (as per your question), just substitute that part of the regex.

Split image path in javascript from first slash

I have the following path :
/data/2/444/test.text
or (without a slash at the start of the path)
data/2/444/test.text
I would like to return in JS the following result :
"/2/444/test.text"
I tried with the following: but I managed only to get the base name
new String(str).substring(str.lastIndexOf('/') + 1);
You can use a simple regex to remove the first directory in the path.
str.replace(/^\/?[^/]+\//, '/')
^\/? Optional slash at the beginning of string.
[^/]+\// match any non slash character until it encounter a slash
const input = ['/data/2/444/test.text', 'data/2/444/test.text', 'file.txt'];
const output = input.map(str => str.replace(/^\/?[^/]+\//, '/'))
console.log(output);
If you only want to replace /data from the beginning you can use:
^\/?data\/
lastIndexOf finds the last occurrence of a string within a string. When you use substring(x) on a string y, it will return the characters of y starting at x. So using lastIndexOf in this use case isn't what you want. You can achieve what you want by using indexOf (finding the first occurrence of a string within a string).
To account for the different formats of your input string (i.e. /data and data), you can just test for that:
function getPathWithoutData(str) {
var strWithoutSlash = str[0] === '/' ? str.substring(1) : str;
return strWithoutSlash.substring(strWithoutSlash.indexOf('/'));
}
You can easily do it without regexes and using slice and indexOf:
const getPath=path=>path.slice(path.indexOf('/',path[0]==="/"?1:0)-path.length);
console.log(getPath('/data/2/444/test.text'));
console.log(getPath('data/2/444/test.text'));
This checks if the first char is a / or not, and adjusts the indexOf accordingly to match either the second or first /. Also note how the subtraction gives a negative value, which gets the intended characters from the end, up to the /.
Of course you can do still do it with substring, as you were attempting, but with indexOf instead of lastIndexOf, because you want the 1st or 2nd / not the last one:
const getPath=path=>path.substring(path.indexOf('/',path[0]==="/"?1:0),path.length);
console.log(getPath('/data/2/444/test.text'));
console.log(getPath('data/2/444/test.text'));
It's worth mentioning that these may not be as robust as a regex, but are simple enough, and may fit your needs, depending on how the data can vary.
You could use String.prototype.split() and pass it a regex.
const paths = [
"/data/path/one",
"data/path/two"
];
const modifyPath = p => {
const [fallback, newPath] = p.split(/\/?data/);
return newPath || fallback;
}
console.log(paths.map(modifyPath));
Or you could use String.prototype.replace()
const paths = [
"/data/path/one",
"data/path/two",
];
const modifyPath = p => {
return p.replace(/\/?data(.*)/, '$1');
}
console.log(paths.map(modifyPath));

Way to replace substring that in regular expression with another

I want to know way to replace substring in url with new string.
https://pbs.twimg.com/media/DW7hPt9VAAAdKE7?format=jpg&name=small
after "&name=" they are many kind of size like
900x900,medium,360x360,small
let href = document.location.href;
if(!href.includes('&name=orig')){
if(href.includes(/900x900|medium|360x360|small/){ //if href have some size in regular expression
// I try to make it search for substring in regular expression
document.location.href = href.replace(/900x900|medium|360x360|small/,'orig');
}
else{ //if url don't have '&name=' like above
var adding = '&name=orig';
document.location.href = link+adding;
}
}
It not working
I don't want to write code to check all case like
if(href.includes('900x900')
if(href.includes('medium')
if(href.includes('360x360')
if(href.includes('small')
they are way to find match at once?
change if(href.includes(/900x900|medium|360x360|small/){ to
if(href.search(/900x900|medium|360x360|small/) !== -1){
as includes accepts the string not the regex.

Get a substring from a string for a regular expression in JavaScript

I have a string of the following form:
data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data
It can be in different languages, but in any case I need to get a string which is between the characters ' '
That is, in the example above, I need to get the following string:
view-7631b26ea80b1b601c313b15cc4e2ab03faedf30
Can I do this using the method string.replace(regexp, str) ?
I've highlighted the desired line using the following regular expression:
/'\b(.*)\b'/gm
Now, using the method string.replace I need to delete everything except that...
Got any suggestions?
Use match method.
var data = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
data = data.match(/'\b(.*)\b'/gm)
You have good solid anchor text in either side, so:
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
Live Example:
var str = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
document.body.innerHTML =
"<pre>Got: [" + substr + "]</pre>";
But again, as I said in a comment, using a simple regular expression to extract information from HTML is usually doomed to fail. For instance, you probably don't want to match this:
<p>The string is data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'</p>
...and yet, a simple regex solution will do exactly that. To properly handle HTML, you must use a proper HTML parser.
You can also try this one:
/\'([^\']+)\'/gm

Finding image url via using Regex

Any working Regex to find image url ?
Example :
var reg = /^url\(|url\(".*"\)|\)$/;
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
var string2 = 'url(http://domain.com/randompath/random4509324041123213.jpg)';
console.log(string.match(reg));
console.log(string2.match(reg));
I tied but fail with this reg
pattern will look like this, I just want image url between url(" ") or url( )
I just want to get output like http://domain.com/randompath/random4509324041123213.jpg
http://jsbin.com/ahewaq/1/edit
I'd simply use this expression:
/url.*\("?([^")]+)/
This returns an array, where the first index (0) contains the entire match, the second will be the url itself, like so:
'url("http://domain.com/randompath/random4509324041123213.jpg")'.match(/url.*\("?([^")]+)/)[1];
//returns "http://domain.com/randompath/random4509324041123213.jpg"
//or without the quotes, same return, same expression
'url(http://domain.com/randompath/random4509324041123213.jpg)'.match(/url.*\("?([^")]+)/)[1];
If there is a change that single and double quotes are used, you can simply replace all " by either '" or ['"], in this case:
/url.*\(["']?([^"')]+)/
Try this regexp:
var regex = /\burl\(\"?(.*?)\"?\)/;
var match = regex.exec(string);
console.log(match[1]);
The URL is captured in the first subgroup.
If the string will always be consistent, one option would be simply to remove the first 4 characters url(" and the last two "):
var string = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
// Remove last two characters
string = string.substr(0, string.length - 2);
// Remove first five characters
string = string.substr(5, string.length);
Here's a working fiddle.
Benefit of this approach: You can edit it yourself, without asking StackOverflow to do it for you. RegEx is great, but if you don't know it, peppering your code with it makes for a frustrating refactor.

Categories