Given
google.com
How can I get .com and save it to a variable. I was thinking of using regex to split google.com into google and ".com", but I don't know the regex to do this.
It might help to know that I got the hostname from using window.location.hostname
Thank you!
You can split the location by ., and the last value in the array will be the extension. Something like this would work:
var extension=location.hostname.split(".");
extension=extension[extension.length-1];
console.log(extension)
JS Fiddle Example: https://jsfiddle.net/igor_9000/yvy0zrat/
Hope that helps!
You could use a substring and lastIndexof:
var str = window.location.hostname;
var ending = str.substring(str.lastIndexOf(".")+1);
https://jsfiddle.net/06r86q0n/
Mostly the same as the other answer, but neither helps with double domains like ".co.uk"
If you are looking to create a regex statement that will only grab the .com then I would use something like:
/(.com)/ as your Regex statement
so your code would look like something like this:
var regexMatch = /(.com)/; as a regular expression literal
or
var regexMatch = new RegExp(".com"); as constructor function of the RegExp object.
Do not use split, its a bit slower than slice:
var last = location.hostname
var t = last.slice(last.lastIndexOf("."));
console.log(t) // .com, .net etc...
Fiddle: https://jsfiddle.net/1qu0rffb/
Related
I've got a string 'url(data:image/png;base64,iVBORw0K...GgoA)'.
I need to invoke only base64 data from it. In output i'd like to see something like this ['iVBORw0K...GgoA'].
Could anyone help me with creating a correct RegExp expression?
Thanks in advance.
.*base64,(\w+)\)$
If you get group 1 from the regex, you will get the base64 data you want.
You can write a regular expression like this,
var phrase = "url(data:image/png;base64,iVBORw0K...GgoA)";
var myRegexp = /base64,(.*)/;
var match = myRegexp.exec(phrase);
alert(match[1]);
HTH
I am desperate - I don't see what I'm doing wrong. I try to replace all occurrences of '8969' but I always get the original string (no matter whether tmp is a string or an int). Maybe it's already too late, maybe I'm blind, ...
var tmp = "8969";
alert("8969_8969".replace(/tmp/g, "99"));
Can someone help me out?
The / characters are the container for a regular expression in this case. 'tmp' is therefore not used as a variable, but as a literal string.
var tmp = /8969/g;
alert("8969_8969".replace(tmp, "99"));
alert("8969_8969".replace(/8969/g, "99"));
or
var tmp = "8969"
alert("8969_8969".replace(new RegExp(tmp,"g"), "99"));
Live DEMO
Dynamic way of handling a regex:
var nRegExp = new RegExp("8969", 'g');
alert("8969_8969".replace(nRegExp, "99"));
/tmp/g. This is a regex looking for the phrase "tmp". You need to use new RegExp to make a dynamic regex.
alert("8969_8969".replace(new RegExp(tmp,'g'), "99"));
Javascript doesn't support that usage of tmp, it will try to use 'tmp' literally, as a regex pattern.
"8969_8969".replace(new RegExp(tmp,'g'), "99")
str = 'http://*.foo.com/bar/' is a valid string.
How do I write a regex to validate this in JavaScript?
`http://xyz.foo.com/bar/` ✓ valid
`http://xyz.foo.com/bar/abc/` ✗ invalid
`http://xyz.foo.com/` ✗ invalid
try playing around at RegExr. It has a lot of good information and will give you a javascript regex at the bottom of the page when you are done.
Try this:
var url = // your url
url.match(/http://[a-zA-Z_0-9]+\.foo\.com/bar/$/g)
The $ matches the end of a string. The $ at the end will make sure that there is no text after it.
Can you give some more details?
e,g, /^http://[a-zA-Z]+.foo.com/bar/$/g
will do what you're looking for if it is on a single line (through ^$ delimiters). It will match xyz but not xyz1 which is easy to fix if you want to include numbers.
Play around it and let me know if you have more questions.
You can assign some of the location object's properties to your own variables once the page loads (since location is free to change afterward):
var hostURL = location.host; //should be '*.foo.com'
var pathURL = location.pathname; //should be '/bar'
Then create a RegExp object:
var regex = '.*\.foo\.com/bar/$';
var testURL = new RegExp(regex);
And test the URL:
if (testURL(hostURL + pathURL)) {
//do something
}
This regex oughta do it for you.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov)\b
Assign the above to a variable. And test if this pattern matches with the url of your choice using test() method of javascript. Update this to suit your needs if need be.
Let's say I have something like this:
var location = '/users/45/messages/current/20/';
and I need to end up with this:
'/45/messages/current/20/'
So, I need to erase the first part of /whatever/
I can use jquery and/or javascript. How would I do it in the best way possible?
To replace everything up to the second slash:
var i = location.indexOf("/", 1); //Start at 2nd character to skip first slash
var result = location.substr(i);
You can use regular expressions, or the possibly more readable
var location = '/users/45/messages/current/20/';
var delim = "/"
alert(delim+location.split(delim).slice(2).join(delim))
Use JavaScript's slice() command. Do you need to parse for where to slice from or is it a known prefix? Depending on what exactly you are parsing for, it may be as simple as use match() to find your pattern.
location.replace("/(whatever|you|want|users|something)/", "");
find the 2nd index of '/' in the string, then substr from the index to the end.
If you always want to eliminate the first part of a relative path, it's probably simplest just to use regular expressions
var loc = '/users/45/messages/current/20/';
loc.replace(/^\/[^\/]+/,'');
location.replace(/\/.*?\//, "/");
location.replace(/^\/[^\/]+/, '')
i want to extract 34 from this string. How Can i done that ? (i will use javascript)
#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30
var src = "#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30",
match = /project_subcategory=(\d+)/g.exec(src);
alert(match[1]);
Anyways, it looks like a query string so there should be a better way to parse/read that string. See http://blog.falafel.com/Blogs/AdamAnderson/07-12-17/Parse_a_Query_String_in_JavaScript.aspx
.*project_subcategory=(\d*).*