expresión regular javascript para extraer la parte media
Frecuentes
Visto 636 veces
0
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?
2 Respuestas
1
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
0
The regular expression you are looking for is this: /^http\://www.google.com/hello/world/(.*/)d.htm$/
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 regex or haz tu propia pregunta.
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. - nneonneo
just the components between 'http:/ /xxx/xxx/xxx' and 'xxx.html' - mmcc