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

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,736
Members
45,708
Latest member
RenaldoFor

Latest Threads

Top