Javascript 如何从URL获取值?
我有点像Javascript 如何从URL获取值?,javascript,regex,Javascript,Regex,我有点像 var url = 'http://stackoverflow.com/questions/24a34b83c72/js-regex-get-values-between-two-characters' 如何使用纯javascript获取24a34b83c72ID?我知道它总是在问题/部分之后,而且不管它是否包含数字或符号,它都需要在下一个/之前结束。我尝试过这样的事情 url.substring(url.lastIndexOf('questions/')) 但这导致了它之后的整个线
var url = 'http://stackoverflow.com/questions/24a34b83c72/js-regex-get-values-between-two-characters'
如何使用纯javascript获取24a34b83c72
ID?我知道它总是在问题/
部分之后,而且不管它是否包含数字或符号,它都需要在下一个/
之前结束。我尝试过这样的事情
url.substring(url.lastIndexOf('questions/'))
但这导致了它之后的整个线程。我尝试了常规表达式,但最接近的是:
var regex=/“详细信息\/”[a-zA-Z0-9]+“\/”/
有人能帮我吗?你可以在
问题/
之后和下一个/
之前对所有内容进行分组,如下所示:
url.match(/questions\/([^/]+)/)[1]
您可以看到url的输出。match(..)
是这样的:
["questions/24a34b83c72", "24a34b83c72"]
第二项在那里,因为
[^/]+
周围有括号,所以如果您坚持使用regexp,则可以使用url.match(…)[1]
访问它:
questions\/([0-9A-Za-z]+?)\/
正则表达式对于更复杂的模式或重复匹配非常有用。你的要求简单而独特 将字符串按
'/'
拆分,找到'questions'
的索引,结果在下一个索引中:
var parts = url.split('/');
var result = parts[parts.indexOf('questions') + 1];
在Firefox控制台上的示例中,这对我很有用:
>> var url = 'http://stackoverflow.com/questions/24a34b83c72/js-regex- get-values-between-two-characters'
>> var regex = /questions\/([a-zA-Z0-9]+)\//
>> regex.exec(url)
Array [ "questions/24a34b83c72/", "24a34b83c72" ]
数组的第二个元素应该是您要查找的元素。[另一个选项]
我首先解释URL字符串,然后解释解释返回给我的内容(作为一个字符串)。当下面的循环正在执行(或解释)时,下面的命令块将吃掉url
字符串,并逐步构造一个具有代表性的数组
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)
}
一旦运行了urlTree
数组,该数组在url
字符串中使用base构建。此外,您的URL当前以数组形式返回以下树:
在代码中。树数组中的每个项都是一个对象。每个对象都有属性类型
<代码>类型显示项目所代表的内容
在此解析中,有以下类型(以字符串形式):
“URLProtocol”
->它可能是http、https或其他任何东西。具有以下属性:值
(协议字符串,如:“http”、“ws”等)
“Name”
->它是某物的名称。示例:name.name。。。name.com?name=0;具有以下属性:值
(名称字符串)
“NextDirectory”
->表示“/”——“打开的新目录”
“获取”
->?起动。“现在可以声明URL变量了”
“变量”
->表示什么?变量具有以下属性:名称
和值
基本的。这就是全部。然后,您可以按照自己的说明使用数字循环来解释数组。
/questions\/([^/]+)/
您提供的示例URL中没有“详细信息”。var result=URL.match(/questions\/(.+?)\/)
将执行正则表达式匹配,该匹配作为匹配组具有所需的输出。在检查匹配是否成功后,可以使用结果[1]
提取输出。也许最好通过斜杠/拆分字符串并获取数组。id之前的密钥作为id存在。Dorry用于我的english@Naumov-您的建议已作为答案发布-您可以通过投票表示支持:-)