Regex javascript to match href - javascript

Logout
That above is what I want to search for.
I want to get h= and t= from that URL, or just get the entire url in href=""
How would I do this with regex?

You should be able to get the href with:
var array_of_matches = str.match(/href="([^"]*")/g)
Look for 'href="' then start a capture group of all non-double-quote characters, until it ends with a final doublequote. You can pull out the query arguments using more groups inside that group.
Look at this javascript regex tutorial. And the global flag to get the array of matches described in the string regex api.

This should return both h and t values:
logout.php\?h=(\w+)&t=(\w+)

/href="[^"]+"/g

Related

How would I write a Regular Expression to capture the value between Last Slash and Query String?

Problem:
Extract image file name from CDN address similar to the following:
https://cdnstorage.api.com/v0/b/my-app.com/o/photo%2FB%_2.jpeg?alt=media&token=4e32-a1a2-c48e6c91a2ba
Two-stage Solution:
I am using two regular expressions to retrieve the file name:
var postLastSlashRegEx = /[^\/]+$/,
preQueryRegEx = /^([^?]+)/;
var fileFromURL = urlString.match(postLastSlashRegEx)[0].match(preQueryRegEx)[0];
// fileFromURL = "photo%2FB%_2.jpeg"
Question:
Is there a way I can combine both regular expressions?
I've tried using capture groups, but haven't been able to produce a working solution.
From my comment
You can use a lookahead to find the "?" and use [^/] to match any non-slash characters.
/[^/]+(?=\?)/
To remove the dependency on the URL needing a "?", you can make the lookahead match a question mark or the end of line indicator (represented by $), but make sure the first glob is non-greedy.
/[^/]+?(?=\?|$)/
You don't have to use regex, you can just use split and substr.
var str = "https://cdnstorage.api.com/v0/b/my-app.com/o/photo%2FB%_2.jpeg?alt=media&token=4e32-a1a2-c48e6c91a2ba".split("?")[0];
var fileName = temp.substr(temp.lastIndexOf('/')+1);
but if regex is important to you, then:
str.match(/[^?]*\/([^?]+)/)[1]
The code using the substring method would look like the following -
var fileFromURL = urlString.substring(urlString.lastIndexOf('/') + 1, urlString.lastIndexOf('?'))

Removing a letters located between to specific string

I want to make sure that the URL I get from window.location does not already contain a specific fragment identifier already. If it does, I must remove it. So I must search the URL, and find the string that starts with mp- and continues until the end URL or the next # (Just in case the URL contains more than one fragment identifier).
Examples of inputs and outputs:
www.site.com/#mp-1 --> www.site.com/
www.site.com#mp-1 --> www.site.com
www.site.com/#mp-1#pic --> www.site.com/#pic
My code:
(that obviously does not work correctly)
var url = window.location;
if(url.toLowerCase().indexOf("#mp-") >= 0){
var imgString = url.substring(url.indexOf('#mp-') + 4,url.indexOf('#'));
console.log(imgString);
}
Any idea how to do it?
Something like this? This uses a regular expression to filter the unwanted string.
var inputs = [
"www.site.com/#mp-1",
"www.site.com#mp-1",
"www.site.com/#mp-1#pic"
];
inputs = inputs.map(function(input) {
return input.replace(/#mp-1?/, '');
});
console.log(inputs);
Output:
["www.site.com/", "www.site.com", "www.site.com/#pic"]
jsfiddle: https://jsfiddle.net/tghuye75/
The regex I used /#mp-1?/ removes any strings like #mp- or #mp-1. For a string of unknown length until the next hashtag, you can use /#mp-[^#]* which removes #mp-, #mp-1, and #mp-somelongstring.
Use regular expressions:
var url = window.location;
var imgString = url.replace(/(#mp-[^#\s]+)/, "");
It removes from URL hash anything from mp- to the char before #.
Regex101 demo
You can use .replace to replace a regular expression matching ("#mp-" followed by 0 or more non-# characters) with the empty string. If it's possible there are multiple segments you want to remove, just add a g flag to the regex.
url = url.replace(/#mp-[^#]*/, '');
The window.location has the hash property so... window.location.hash
The most primitive way is to declare
var char_start, char_end
and find two "#" or one and the 2nd will be end of input.
with that... you can do what you want, the change of window.location.hash will normally affect the browser adress.
Good luck!

How to get youku video id from url by regex?

I need to get youku video id from url by regex, for example:
http://v.youku.com/v_show/id_XNTg3OTc3MzY4.html
I only need XNTg3OTc3MzY4 to keep in a variable.
How can I write it in function below
var youkuEmbed = "[[*supplier-video]]";
var youkuUrl = youkuEmbed.match(/http://v\.youku\.com/v_show/id_(\w+)\.html/);
I tried this but it didn't work.
Thanks!
You can use a simple regex like this:
id_(\w+)
Working demo
The idea is to match the _id and the capture all the alphanumeric strings.
MATCH 1
1. [29-42] `XNTg3OTc3MzY4`
If you go the Code Generator section you can get the code. However, you can use something like this:
var myString = 'http://v.youku.com/v_show/id_XNTg3OTc3MzY4.html';
var myRegexp = /id_(\w+)/;
var match = myRegexp.exec(myString);
alert(match[1]);
//Shows: XNTg3OTc3MzY4
You can use this regex:
http://v\.youku\.com/v_show/id_(\w+)\.html
Your match is in the first capturing group.
Here is a regex demo.
Id the id always follows id_, you could possibly split the string.
'http://v.youku.com/v_show/id_XNTg3OTc3MzY4.html'.split(/.*id_|\./)[1]
//=> 'XNTg3OTc3MzY4'
For this specific string, you could just do.
'http://youku.com/id_XNTg30Tc3MzY4.html'.split(/id_|\./)[2]
//=> 'XNTg3OTc3MzY4'
It looks like you need to escape all the slashes because that's the delimiter for the regex itself:
var youkuUrl = youkuEmbed.match(/http:\/\/v\.youku\.com\/v_show\/id_(\w+)\.html/);
Then use the first capture group, as Unihedron stated.

Regex to get a specific query string variable in a URL

I have a URL like
server/area/controller/action/4/?param=2"
in which the server can be
http://localhost/abc
https://test.abc.com
https://abc.om
I want to get the first character after "action/" which is 4 in the above URL, with a regex. Is it possible with regex in js, or is there any way?
Use regex \d+(?=\/\?)
var url = "server/area/controller/action/4/?param=2";
var param = url.match(/\d+(?=\/\?)/);
Test code here.
Using this regex in JavaScript:
action/(.)
Allows you to access the first matching group, which will contain the first character after action/ -- see the examples at JSFiddle
This way splits the URL on the / characters and extracts the last but one element
var url = "server/area/controller/action/4/?param=2".split ('/').slice (-2,-1)[0];

Simple javascript regex

I need: www.mydomain.com:1235 form the text var below:
var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/[^]+\//));
output is: //www.mydomain.com:1235/
How do I exclude the delimiters?
You need to use parens to group what you want to match. Then, the call to .match() will let you use indexers. Index 0 is the whole string match, and index 1 is the first paren grouping.
var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/([^\/]+)\//)[1]);
Not a regex, but you could do this:
Example: http://jsfiddle.net/nTmv9/
text = text.split('http://')[1].split('/')[0];
or with a regex:
Example: http://jsfiddle.net/nTmv9/1/
text = text.match(/http:\/\/([^\/]+)\//)[1];
This will capture the domain without the http or the url slugs.
https?:\/\/([^\/]+)\/
If you need help figuring out regex here is a great tool I use all of the time.
http://gskinner.com/RegExr/
Cheers

Categories

Resources