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];
});
Related
function searchString(user_input, search) {
var user_string = document.getElementById('user_input');
var search_string = document.getElementById('search');
document.write(search_string.value);
}
The document would'nt print the value.I am new to javascript and finding it hard to figure out why? I also tried this:
function searchString(user_input, search) {
var user_string = document.getElementById('user_input');
var search_string = document.getElementById('search').value;
document.write(search_string);
}
But no results. I am a noob, please help?
Don't use document.write. Add jQuery to your site instead, then you'd simply do something like:
$('#search').val(search_string);
And you'd get the values using:
var test = $('#searchBox').val();
If you want to do something professional grade I recommend getting into Vue.js as early as possible, it's essentially built to tackle this specific type of problem.
Doing "raw" javascript is sometimes a good thing but don't make things difficult for you. When you have to edit the DOM, use jQuery. If you want to build a website that is highly responsive to various data on your website, use Vue.js. Don't use raw javascript if you're a noob.
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
Hello everyone i would like to do this. I have a javascript variable which has this kind of value [section][row][/row][/section] I would like to convert this into an javascript array. do anyone has any clue about this. I just like an array like
[0]=>[section]
[1]=>[row]
[2]=>[/row]
[3]=>[/section]
Please Help me to do this.
Using some regex:
var tags = '[section][row][/row][/section]'.match(/\[\/?\w+]/g);
tags; // ["[section]", "[row]", "[/row]", "[/section]"]
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
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];