expresión regular javascript para extraer la parte media

Tengo una cadena que se parece a esto: "http://www.example.com/hello/world/ab/c/d.html" (o esto: "http://www.example.com/hello/world/ab/d.html")

I want to extract the content between http://www.example.com/hello/world/ y d.html. What should the generic regular expression be?

preguntado el 25 de agosto de 12 a las 06:08

Depends on what you mean by 'middle'. In this case, you are asking for the 3rd and 4th path components -- hardly a 'generic' definition of middle. -

just the components between 'http:/ /xxx/xxx/xxx' and 'xxx.html' -

2 Respuestas

Probablemente quieras

/^http:\/\/[^\/]*\/[^\/]*\/[^\/]*\/(.*)\/[^\/]*$/

This (complicated-looking) expression skips the domain and the first two path components, then extracts all the bits before the final path component.

Ejemplo:

>>> 'http://www.google.com/hello/world/ab/c/d.html'.match(/^http:\/\/[^\/]*\/[^\/]*\/[^\/]*\/(.*)\/[^\/]*$/)
["http://www.google.com/hello/world/ab/c/d.html", "ab/c"]

Respondido 25 ago 12, 06:08

The regular expression you are looking for is this: /^http\://www.google.com/hello/world/(.*/)d.htm$/

http://jsfiddle.net/rDvJK/

function getIt(fromWhat) {
    var matches = fromWhat.match(/^http\:\/\/www\.google\.com\/hello\/world\/(.*\/)d.htm$/);
    console.log(matches);
    return matches[1];
}
getIt("http://www.google.com/hello/world/ab/c/d.htm");

Respondido 25 ago 12, 06:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.