Reputation: 781
I am trying to get a couple values out of a url using regular expression. Since i am not familiar i was hoping someone could guide me .
I have a url i am trying to manipulate looks like this .
var urlString = "/myMessages/v2/messages/I15/parts/0"
The values i am trying to get are I15 and 0 . These values could be anything but the format of urlString would remain the same. What is the best and most efficient way to go about obtaining these values?
Upvotes: 0
Views: 108
Reputation: 5271
\/messages\/([^\/]+)\/parts\/(.*)
Then use the first and second matched groups.
Upvotes: 0
Reputation: 1326
I'd suggest a slightly more robust solution than what's been proposed so far:
var urlString = "<url string coming in some undetermined order>";
var parts = urlString.split('/');
var map = {};
for (var i = 0; i < parts.length; i += 2) {
map[parts[i]] = parts[i+1];
}
This way your values are now stored along with the keys to access them, which you can set in any way you want.
So say your URL is /myMessages/v2/partnum/2/messages/I18/thing/splund/foo/bar
and you want the values of partnum
, thing
, and foo
, but you don't necessarily know that they'll be in the order I just put up in that url. Using the method I suggested, you can simply access their values using:
var partnum = map['partnum'] // '2'
var thing = map['thing'] // 'splund'
var foo = map['foo'] // 'bar'
Upvotes: 2
Reputation: 3294
I'm not sure I would use regex for this. I would create a custom function to handle urls of this type:
function handleUrlTokens(url){
var parts = url.split('/').filter(function(o){return o!==''})
var params = {};
for (var i = 0; i < parts.length; i+=2) {
param[parts[i]] = parts[i+1];
}
return params;
}
handleUrlTokens("/myMessages/v2/messages/I15/parts/0") //Object {myMessages: "v2", messages: "I15", parts: "0"}
This would then be able to handle many different types of url, as long as the convention is /key/value/key/value.
Edited because revenProdigalKnight's loop was better than mine.
Upvotes: 2
Reputation: 149058
For something this simple, a straightforward solution is to use split
instead:
var urlString = "/myMessages/v2/messages/I15/parts/0";
var parts = urlString.split('/');
var value1 = parts[4]; // "I15"
var value2 = parts[6]; // "0"
This will work just fine as long as you know exactly how many /
characters will appear before the parts you're interested in.
If you also know that the URL will always begin with "/myMessages/v2/messages/"
, you might be able to make this a bit more efficient:
var urlString = "/myMessages/v2/messages/I15/parts/0";
var substring = urlString.substr(24),
i = substring.indexOf('/'),
j = substring.indexOf('/', i);
var value1 = substring.substr(0, i); // "I15"
var value2 = substring.substr(j + 1); // "0"
However, efficiency really shouldn't be your first priority. You should optimize for readability.
Upvotes: 1
Reputation: 2130
var urlString = "/myMessages/v2/messages/I15/parts/0"
var reqArray = urlString.split('/');
console.log(reqArray[4],reqArray[6]);
This is the better way, when compared to regular expression.
It works fine for me!!
Upvotes: 0
Reputation: 2569
var first = urlString.split('/')[4];
var second = urlString.split('/')[6];
Note that this will not work if the order is changed.
Upvotes: 0