Java Script Regular Expressions

D

Dempsey.Jeff

I am a wreck at regular expressions so I could use a little help.

Say I have a url that looks something like this.
http://test.com/test/test2/tabid/656/Default.aspx. I need to be able
to pull out only the word test2. I wont know that the word is going
to be test2 when I use the regular expressions so I need to find
everything between / and /tabid because that is the only thing that
will be the same no matter what.
 
E

Evertjan.

wrote on 14 feb 2008 in comp.lang.javascript:
I am a wreck at regular expressions so I could use a little help.

Say I have a url that looks something like this.
http://test.com/test/test2/tabid/656/Default.aspx. I need to be able
to pull out only the word test2. I wont know that the word is going
to be test2 when I use the regular expressions so I need to find
everything between / and /tabid because that is the only thing that
will be the same no matter what.

<script type='text/javascript'>

var t = 'http://test.com/test/test2/tabid/656/Default.aspx';

t = t.replace(/.+\/([^\/]+)\/tabid\/.+/,'$1')

alert(t);


</script>
 
G

Gregor Kofler

(e-mail address removed) meinte:
I am a wreck at regular expressions so I could use a little help.

Say I have a url that looks something like this.
http://test.com/test/test2/tabid/656/Default.aspx. I need to be able
to pull out only the word test2. I wont know that the word is going
to be test2 when I use the regular expressions so I need to find
everything between / and /tabid because that is the only thing that
will be the same no matter what.

Untested:
/\/(.*?)\/tabid/

Gregor
 
L

Lasse Reichstein Nielsen

Say I have a url that looks something like this.
http://test.com/test/test2/tabid/656/Default.aspx. I need to be able
to pull out only the word test2. I wont know that the word is going
to be test2 when I use the regular expressions so I need to find
everything between / and /tabid because that is the only thing that
will be the same no matter what.

Others have suggested regular expression that will work.

However, before going for a regular expression that you might not
completely understand, you could consider doing the lookup manually:

function getDirBeforeTabid(url) {
var matchEnd = url.indexOf("/tabid");
var prevSlashIndex = url.lastIndexOf("/", matchEnd - 1);
var matchStart = prevSlashIndex + 1;
return url.substring(matchStart, matchEnd);
}

Regular expressions are cool and versatile, but they often become
a golden hammer.

/L '/\/([^/]*)\/tabid/'
 
T

Thomas 'PointedEars' Lahn

Gregor said:
(e-mail address removed) meinte:

Untested:
/\/(.*?)\/tabid/

Less error-prone because not using the non-greedy subexpression and not
allowing the empty string:

/\/[^\/]+\/tabid/


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top