Find and replace regex JavaScript - 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

Related

How to Split many Strings with Jquery

Let's say I have a bunch of strings which I get from user input. Each strings starts with a new line like the following example:
gordon:davis
harley:vona
empir:domen
furno:shyko
I would like to write a function in jQuery which loads the user data from the form and splits each row's sting. However I would ONLY need the first part of the strings, like:
gordon
harley
empir
furno
Is there any simple solution for this?
I have found something called: $.each(split) but I didn't get it work.
I'm sorry I'm a really newbie in Jquery, I hope you guys can help me out! Thanks in advance!
Use String.prototype.split method.
"gordon:davis".split(":") will return ["gordon","davis"]
Based on this, using Array.prototype.map or a JQuery version :
var strings = ["gordon:davis", "harley:vona"];
var splitStrings = strings.map(function (string) {
return string.split(":")[0];
});

What would be the regex to get the view code from youtube urls?

I just need to get the view code from youtube urls. The api is returning back strings that look like this:
http:\/\/www.youtube.com\/watch?v=XODUrTtvZks&feature=youtube_gdata_player
I need to get this part:
XODUrTtvZks
from the above, keep in mind that sometimes there may be additional parameters after the v=something like:
&feature=youtube_gdata_player
and sometimes there may not be. Can someone please provide the regex that would work in this situation and an example of how to use it using javascript?
You can use /v=([^&]+)/ and get the match at offset 1.
This snippet only matches on URL's from youtube.com:
var url = 'http://www.youtube.com/watch?v=XODUrTtvZks&feature=youtube_gdata_player';
var matches = url.match(/^http[s]?:\/\/www.youtube.com\/watch\?\s*v=([^&]+)/i);
if (matches) {
var videoID = matches[1];
// do stuff
}
You can use an online tool called RegExr to get your regular expression ,[http://gskinner.com/RegExr/].
Regards
Rahul
This snippet is from Google’s own parser at closure:
function getIdFromUrl(url) {
return /https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube\.com\/watch\?)((?:[\w\d\-\_\=]+&(?:amp;)?)*v(?:<[A-Z]+>)?=([0-9a-zA-Z\-\_]+))/i.exec(url)[2];
}
You can see it here:
http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/ui/media/youtube.js?r=1221#246

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');

How to use this with JQuery

how do i use this code with jquery,
i know it's easy but it doesn't work for me.
document.getElementsByTagName('html')[0].innerHTML.replace(/<!--|-->/g,'')
$('html')[0].innerHTML.replace(/<!--|-->/g,'');
If your intention is to generate a new string where all <!-- and --> are removed then your code works just fine. If not then you probably should be reminded that in javascript, a String's replace() method does not replace anything in that string, it generates a new string. So:
var html = document.getElementsByTagName('html')[0];
html.innerHTML = html.innerHTML.replace(/<!--|-->/g,'');
But your question is very odd. Why would you want to do this? Perhaps you want to uncomment some commented out code? This does not look like something that is safe to do.
This should work for you. Remember you can use javascript and jQuery together. Try this:
$("<html>").html($("<html>").html().replace(/<!--|-->/g,''));
Probably not the most elegant solution, but should work. Are you familiar with:
http://visualjquery.com/

Categories

Resources