regular expression for link

i need to trim this string -> http://hokuspokus/ixdebude/ix and i exactly Need this part of the string /ixdebude/ix

I have 2 expressions to solve my Problem.

My first regex ->

/(.*)\/(.*)(\/)(.*)\/*/g returns Array[5] groups .

my second regex ->

/(\/............)$(.*?)$/g returns /ixdebude/ix

Can someone of you give me another regex, a better without thousand dots ?

Answers:

Answer

You could use a regex like this:

//.*?(/.*)

Working demo

For javascript, you could use:

var re = /\/\/.*?(\/.*)/; 
var str = 'http://hokuspokus/ixdebude/ix';
var m;

if ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    // View your result using the m-variable.
    // eg m[0] etc.
}
Answer

You could try this

var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";

parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port;     // => "3000"
parser.pathname; // => "/pathname/"
parser.search;   // => "?search=test"
parser.hash;     // => "#hash"
parser.host;     // => "example.com:3000"

https://gist.github.com/jlong/2428561

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.