javascript match and add string in dynamic Url - javascript

I have a dynamic URL, which i want to append some string at last
'http://staging.mydomain.name.com/test/7bb12c5f7b2f4f008261bea2d3f5abd2/200x200.png'
want to append "preview" before size (200x200.png which is also dynamic), something like below
'http://staging.mydomain.name.com/test/7bb12c5f7b2f4f008261bea2d3f5abd2/preview/200x200.png'
I have seen Javascript match to remove part of file name from URL - replace the last occurence but its not what i want.
Thanks

Just use a simple regular expression like:
'http://.../200x200.png'.replace(/[^\/]+$/, 'preview/$&')

You can do like str.replace(/(/\d+x\d+.)/,'/preview$1')

Related

optional part for String.replace in javascript

I'm doing something like this:
ErjaView.ServiceListData.replace(`${detail.ID},${detail.Count}#`,"")
I just want to make the hashtag (#) optional.
I want to replace it if my string have one but if it doesn't, I still want to replace my string. how can I do this?
condition like this: replace the string if it does have such form. but if you couldn't find # do it anyways.

How to replace last href occurrence in a string?

The text is something like the following
Stuff
More stuff
more
last thing
more
I need to replace what's inside the last href ocurrence in the text, with another reference as http://realreference.com/real?the one to replace
I can actually change the href of all ocurrences in the string with the global flag g after the regex /href="(.*?)"/ and a function like the following:
string.replace(/href="(.*?)"/g, () => {
return `href="http://realreference.com/real?the one to replace"`;
})
I would need to only change it in the last href ocurrence of the string, which in this case it's href="http://example.com/more?lasthing goes here"
If you are looking for a pure regex solution, you can use the following to find the last match:
pattern(?![\s\S]*pattern)
Example: href="(.+?)"(?![\s\S]*href="(.+?)")
See it yourself: regex101.com
Find all href with match and replace only last
You can use querySelector in order to query the desired element in the DOM. Using :last-of-type will make it select the last element of the type you select. Therefore, with this code in the first line, you will get the last <a>. After you get the element, you can replace its href attribute by using setAttribute.
let last_a = document.querySelector('a:last-of-type');
last_a.setAttribute('href', 'http://realreference.com/real?the one to replace');
console.log(last_a);
Stuff
More stuff
more
last thing
more

Insert one string into another

I need to insert 'embed' into the middle of a YouTube URL, so e.g.
https://www.youtube.com/watch?v=JDCV_cK1L1A
Needs to be modified to:
https://www.youtube.com/embed/watch?v=JDCV_cK1L1A
Using only jQuery.
Do I need to use RegEx for this? It seems the only answers I can find online are either appending / prepending, or inserting a string into x position of another string... but neither solution are appropriate for what I need to achieve
You don't need a regular expression for this. If you want to add the folder right after the domain, you can use a plain replace:
url = url.replace('youtube.com/', 'youtube.com/embed/');
If you want to add it as the last folder, you can find the last slash and insert it there:
var last = url.lastIndexOf('/');
url = url.substr(0, last) + '/embed' + url.substr(last);

Replace everything after last character in URL

I have the following code which replaces the current URL using JavaScript:
window.location.replace(window.location.href.replace(/\/?$/, '#/view-0'));
However if I have a URL like:
domain.com/#/test or domain.com/#/
It will append the #/view-0 to the current hash. What I want to is replace EVERYTHING after the last part of the URL including any query strings or hashes.
So presume my regex doesn't handle that... How can I amend it, to be more aggressive?
The following syntax may help:
location.href.replace(/[?#].*$/, '#/view')
It will replace everything after (and together with) ? or # in the string with #/view.
(^[^\/]*?\/)(?:.*)
Use this.Replace by \1 then your string
See demo.
http://regex101.com/r/sA7pZ0/28

Javascript regex to get src url between script tag

I wanted to get name of the script from such a string:
var text = '<script src="scripts/044c7c5e.vendor.js"></script><script src="scripts/fa9f85fb.scripts.js"></script>'
I wanted to retrieve the second script name i.e. fa9f85fb.scripts. How can I achieve this using javascript regex?
I'm writing something like this:
text.match(new RegExp(/<script src="scripts\/[(.*?)]\.scripts\.js"><\/script>/), 'g')[0]
But its returning the whole string.
Your pattern grabbing is a bit off; [(.*?)] should instead be (.*?) simply:
/<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g
will be the entire regex, no need to call the RegExp class constructor either. The matched string is stored at index 0. The various segments are then stored from index 1 onwards.
text.match( /<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g )[1]
Try /\w+.scripts(?=.js)/ ?
Reference: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Your match pattern is a bit vague. I can simply use /fa9f85fb.scripts/ to match it.

Categories