Forum Moderators: open

Message Too Old, No Replies

Removing sections of a chraracter string

         

stefansavva

5:58 am on Mar 6, 2004 (gmt 0)

10+ Year Member



Hello,

Just wondering if anyone knew how to remove sections of a character string using Javascript.

For example, I have three URLs that I enter into a text box and I want to remove everything after the / by pressing submit.

So this;
www.widgets.com/about.html
www.redwidgets.com/
www.bluewidgets.com/faq.html

Would become;
www.widgets.com
www.redwidgets.com
www.bluewidgets.com

Any help would be much appreciated.

Stefan

dcrombie

11:39 am on Mar 6, 2004 (gmt 0)



Something like this?

var newString = inputString.substring (0, inputString.indexOf ('/'));

hyperbole

10:40 pm on Mar 7, 2004 (gmt 0)

10+ Year Member



assume the url is in the variable url.

var pattern = /\/.*$/;
new_url = url.replace(pattern, "");

Purple Martin

11:08 pm on Mar 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hyperbole is on the money. In case you don't recognise that solution, it uses Regular Expressions. More info:
[regular-expressions.info...]