Replace part of string with other text? - javascript

I have a variable
link = 'dog.jpg'
How do I write code to change link to = dog.webm instead?
I've tried link.text.replace('jpg', 'webm'); but it has no effect?
https://jsfiddle.net/m5Lp6a1z/

You need to make an actual assignment back to the link variable:
link = link.replace('jpg', 'webm');
But actually, a regex replacement targeting only JPEG extensions would be probably be safer here:
link = link.replace(/\.jpg$/, '.webm');

If you want to Replace Text Then you need to try this,
let link = 'dog.jpg'
link.replace('jpg', 'webm')
you don't need to write link.text.replace
It will help you.

Related

Javascript find replace content that has "/" in the search string

I have javascript code which finds and replaces all instances of a value on a page. I am working with a CMS so unfortunately I need to work around lack of flexibility with the base HTML.
document.body.innerHTML = document.body.innerHTML.replace(/VALUE/g, new_value);
I want to find a replace a url link using the same process. How do I take into account for the"/" in the search and replace strings?
Example:
find_str = "www.mysite.com/folder";
replace_str = "app.mysite.com/page";
document.body.innerHTML = document.body.innerHTML.replace(/find_str/g, replace_str);
Thank you both for your replies. I have taken advice from both and changed my code as follows:
jQuery('a[href="tttps://www.mysite.com/folder"]').attr("href", "tttps://app.mysite.com/page");
PS: I cannot add https:// to the above as it will not let me save, so I added tttps:// instead.

Find and replace regex JavaScript

I know this is a super easy question, but I can't seem to wrap my head about it. I've got a bunch of URLs in varying languages such as:
www.myurl.com?lang=spa
www.myurl.com?lang=deu
www.myurl.com?lang=por
I need to create buttons to quickly switch from any language extension (spa, por, deu, rus, ukr, etc) to another language. I have the following code so far:
var url = window.location.toString();
window.location = url.replace(/lang=xxx/, 'lang=deu');
I just can't figure out the 3-character wildcard character. I know that I need to do some sort of regular expression or something, I'm just not sure how to go about it. Any help?
Thanks in advance
You can use
([&?]lang=)\w+
This will work with urls like www.myurl.com?foo=bar&lang=por&bar=foo too.
Instead of lang=deu, you'll have to replace with $1deu.
Try ... or .{3} or \w{3} or even [a-z]{3}, depending on how specific you want to be.
var s = 'www.myurl.com?lang=spa';
s.replace(/lang=[a-z]{3}/, 'lang=deu');
// => "www.myurl.com?lang=deu"
Here's a railroad diagram of the above example:
Use /lang=[a-z][3}/, here's an example:
/lang=[a-z]{3}/
Debuggex Demo

javascript replace with regex img tag with specific id but other attributes could be anything

Embarrassed to ask, since I should be able to get this, but I've been hitting my head against the wall for a while. I need to replace an img element with a known unique id (here, say, id="abc123"). I would think this should do it, but apparently I'm wrong:
var rgx = '/<img[^>]*id="abc123"[^>]*>/';
var replaced_text = edata.replace(rgx, myreplacementstring);
where edata is a big chunk of html, and myreplacementstring is what I want to replace the img element with. I know in advance that the image element to replace is all lower case, but of course there will be other attributes beyond the id, and they could be on either side of the id. Should be easy right? What am I missing?
Your regular expression works fine.
What you need to do is this:
var rgx = /<img[^>]*id="abc123"[^>]*>/;
instead of:
var rgx = '/<img[^>]*id="abc123"[^>]*>/';
You can see your code working here:
http://jsfiddle.net/Fresh/bMKLU/

Getting part of referring url with jquery

I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew
You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];

Regular Expression for relative links ONLY

I'm creating a javascript that checks for links in the DOM and changes those who are NOT absolute links. Unfortunately I'm not having any luck...
I would like to match only the first type of links below, and add a folder path
link
<a href"http://somesite.net/somepage.html">link</a>
I've used string.replace(/a.+href="([^http]+)"/, 'path'+$1); to no avail...
Can someone help me here? Thanks in advance.
If the regular expression that you've written to solve a problem using just regular expressions starts to look like overkill, then it is probably overkill. Sometimes a simple if statement used in conjunction with regular expressions can do wonders:
$("a").each(function () {
if (!/^http:\/\//.test(this.href)) {
this.href = "http://example.com/folder/" + this.href; // etc.
}
});
You may want to look at the <base> html tag, instead. It allows you to set the path to which all links and images are relative.
http://www.w3schools.com/tags/tag_base.asp
http://www.w3.org/TR/html5/semantics.html#the-base-element
You've created a character class with the square brackets. Remove them. You want a "negative lookbehind", see comment below for info on syntax. Not all languages support this regex feature though.
Javascript doesn't support lookbehind. This may help though: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
You can use
string.replace(/(a.+href=)"(?!http)(.+)"/gi, '$1"path/$2"')
for example sake, I just made a variable with a couple links in it. You can easily adapt the .replace() to work with however you get the links.
var content = 'linklinklink';
// whatever you want to prefix link with
var base='http://somsite.net';
content = content.replace(/(href=")(?!https?:\/\/)([^"]*)/gi,'$1'+base+'/$2').replace(/\/+/g,'/');
Thanks everyone.
I was able to replace relative paths ONLY by using the following syntax:
var basepath = "pathto/";
var html = html.replace(/(<(a|img)[^>]+(href|src)=")(?!http)([^"]+)/g, '$1'+basepath+'$4');

Categories

Resources