[Un'altra opzione]
mi piacerebbe interpretare la stringa URL prima e dopo interpretare ciò che l'interpretazione ritorna per me (come un Abtract syntax tree). Il seguente blocco di comandi mangerà la stringa url
mentre il ciclo sottostante è in esecuzione (o nell'interpretazione di), costruendo una matrice rappresentativa in passaggi!
var curChar,
directoryPassed,//not yet
expectGetValue,
getContent="",
getStarted,//? not started ($_GET)
inPort,//not reading port
portRead,//port not read yet
state=0,//first state
text="",
urlTree=[];
for(;;){
curChar=url.charAt(0);//first char
if(state===0){
//expects http... ws... file or https, for example.
if(curChar===""){
throw new Error("http:// expected.")
}else if(curChar===":"){
if(directoryPassed){
throw new Error("Unexpected token.")
}
urlTree.push({type:"URLProtocol",value:text});
text="";
state=1
}else{
text+=curChar
}
}else if(state===1){
//expects //...
if(url.substring(0,2)==="//"){
state=2;
url=url.substring(1)
}else if(curChar===""){
throw new Error("// expected.")
}
}else{
//expects anything correct: site.com/dir, localhost:8080, ?get=0, etc.
if(getStarted){
if(curChar==="="){
if(text.length===0){
throw new Error("Unexpected token.")
}else{
expectGetValue=true;
getContent=""
}
}else if(curChar==="&"){
if(expectGetValue||text.length!==0)
urlTree.push({type:"Variable",name:text,value: (getContent||true) });
expectGetValue=false;
text=""
}else if(curChar===""){
if(expectGetValue||text.length!==0)
urlTree.push({type:"Variable",name:text,value: (getContent||true) });
break
}else{
if(expectGetValue){
getContent+=curChar
}else{
text+=curChar
}
}
}else if(curChar==="."){
if(text.length===0){
throw new Error("Unexpected token.")
}else{
if(inPort){
throw new Error("Unexpected token in port.")
}else{
urlTree.push({type:"Name",value:text});
text=""
}
}
}else if(curChar===""){
if(text.length!==0){
if(inPort){
urlTree.push({type:"Port",value:text})
}else{
urlTree.push({type:"Name",value:text})
}
text=""
}else if(inPort){
throw new Error("Port not specified.")
}
break
}else if(curChar==="?"){
//$_GET starts here.
if(text.length!==0){
if(inPort){
urlTree.push({type:"Port",value:text})
}else{
urlTree.push({type:"Name",value:text})
}
text=""
}
getStarted=true;
urlTree.push({type:"Get"})
}else if(curChar==="/"){
if(text.length===0){
throw new Error("Unexpected token.")
}else{
directoryPassed=true;
if(inPort){
inPort=false;
urlTree.push({type:"Port",value:text})
}else{
urlTree.push({type:"Name",value:text})
}
text="";
urlTree.push({type:"NextDirectory"})
//New directory!
}
}else if(curChar===":"){
if(portRead||text.length===0){
throw new Error("Unexpected token.")
}else{
urlTree.push({type:"Text",value:text});
text="";
inPort=
portRead=true;
//Now the port will never be defined again.
}
}else if(inPort){
if(/[0-9]/.test(curChar)){
text+=curChar
}else{
throw new Error("Invalid port token.")
}
}else{
text+=curChar
}
}
url=url.substring(1)
}
Una volta che è correva, si ottiene la matrice urlTree
costruito con base nella stringa url
.Additionaly, l'URL attualmente restituisce il seguente albero in un array:

nel codice. Ogni elemento dell'array tree è un oggetto. Ogni oggetto ha la proprietà type
. type
dice cosa rappresenta l'elemento.
In questo analisi, ci sono questi tipi (in stringa):
"URLProtocol"
-> E 'forse http, https o qualsiasi altra cosa. Ha la proprietà: value
(stringa di protocollo, come: "http", "ws", ecc.)
"Name"
-> È il nome di qualcosa. Esempio: nome.nome ... nome.com ...? Nome = 0; Ha la proprietà: value
(stringa del nome)
"NextDirectory"
-> Rappresenta "/" - "Una nuova directory aperto"
"Get"
->? iniziato. "Ora le variabili URL possono essere dichiarate"
"Variable"
-> rappresenta? variabile. Ha le proprietà: name
e value
;
Base. È tutto. Quindi è possibile interpretare la matrice con un ciclo numerico con le proprie istruzioni.
'/ questions \/([^ /] +) /' https://regex101.com/r/kO9pK4/1 – zerkms
Non ci sono "dettagli" nell'URL di esempio che hai fornito. – jmoerdyk
'var result = url.match (/ questions \/(. +?) \ //)' eseguirà una corrispondenza regolare che ha l'output desiderato come gruppo abbinato. Dopo aver verificato che la corrispondenza abbia avuto successo, è possibile estrarre l'output con 'result [1]'. –