简体   繁体   English

如何使用javascript从此URL中提取数据?

[英]How do I extract data from this URL using javascript?

I need to build a string from the data contained in this url using javascript/jQuery: 我需要使用javascript / jQuery从这个url中包含的数据构建一个字符串:

http://www.example.com/members/admin/projects/?projectid=41

The string returned should look as follows: 返回的字符串应如下所示:

/ajax/projects.php?projectid=41

Obviously if there is no query string present, the method should still return a string of the same format minus the query string. 显然,如果不存在查询字符串,该方法仍应返回相同格式的字符串减去查询字符串。 eg 例如

http://www.example.com/members/admin/messages/

should return... 应该回来......

/ajax/messages.php

I've made numerous attempts, all met without success due to my poor grasp of regular expressions, and it feels as though the ore I rad on the subject the more I am confusing myself. 我做了很多尝试,由于我对正则表达式的把握很差,所有人都没有成功,并且感觉好像我对这个主题的影响我越是困惑自己。

If someone could help it would be greatly appreciated. 如果有人可以提供帮助,将不胜感激。

EDIT: The 'admin' portion of the url is a users 'username' and could be anything. 编辑:网址的“管理员”部分是用户的“用户名”,可以是任何内容。

I know exactly what you are trying to do. 我确切地知道你要做什么。 In order to do it your way just split your string on question mark and then use last item form your array. 为了做到这一点你只需将你的字符串分成问号,然后使用最后一项来形成你的数组。

var data = your_url.split('?');
var  newUrl = '/ajax/projects.php' + (data.length > 1 ? data[length-1] : "");

and you will have your url. 你会得到你的网址。

But what you can do is execute same url using your Script just add one parameter IsAjax=true and then check it in codebehind and execute your ajax logic. 但你可以做的是使用你的脚本执行相同的url只需添加一个参数IsAjax = true然后在代码隐藏中检查它并执行你的ajax逻辑。

eg 例如

$('#somelink').onclick(function(){
   $.ajax({ url: $(this).href, data { IsAjax: true } .... }
});

Using this way you will have more robust app. 使用这种方式,您将拥有更强大的应用程序。

Here's a function that will take your URL and return a new one according to the rules you've listed above: 这是一个函数,它将根据您在上面列出的规则获取您的URL并返回一个新URL:

function processURL(url) {
    var base = "", query = "";
    var matches = url.match(/([^\/\?]+)(\/$|$|\?|\/\?)/);
    if (matches) {
        base = matches[1];
        matches = url.match(/\?[^\?]+$/);
        if (matches) {
            query = matches[0];
        }
    }
    return("/ajax/" + base + ".php" + query);
}

And, a test app that shows it working on a bunch of URLs: http://jsfiddle.net/jfriend00/UbDfn/ 并且,一个测试应用程序,显示它在一堆URL上工作: http//jsfiddle.net/jfriend00/UbDfn/

Input URLs:

var urls = [
    "http://www.example.com/members/admin/projects/?projectid=41",
    "http://www.example.com/members/bob/messages/",
    "http://www.example.com/members/jill/projects/",
    "http://www.example.com/members/alice/projects?testid=99",
    "http://www.example.com/members/admin/projects/?testid=99"
];

Output results:

/ajax/projects.php?projectid=41
/ajax/messages.php
/ajax/projects.php
/ajax/projects.php?testid=99
/ajax/projects.php?testid=99

To explain, the first regular expression looks for: 为了解释,第一个正则表达式寻找:

a slash
followed by one or more characters that is not a slash and not a question mark
followed by one of the four sequences
    /$    a slash at the end of the string
    $     end of the string
    ?     a question mark
    /?    a slash followed by a question mark

The point of this regex is to get the last segment of the path that comes before either the end of the string or the query parameters and it's tolerant of whether the last trailing slash is there or not and whether there are any query parameters. 这个正则表达式的要点是获取字符串结尾或查询参数之前的路径的最后一段,并且它是否容忍最后一个尾部斜杠是否存在以及是否有任何查询参数。

I'll assume that by 我会假设

http://www.example.com/members/admin/messages/

should return... 应该回来......

/ajax/members.php

you meant - should return... 你的意思是 - 应该回来......

/ajax/messages.php

If that is the case try 如果是这样的话试试

var query = url.split('?');
var paths = query[0].split('/');
var path = paths.pop();

if (path == '')  //when there is trailing slash
    path = paths.pop();

if (query.length == 1) //no query string
    newurl = '/ajax/' + path + '.php';
else //query string
    newurl = '/ajax/' + path + '.php?' + query[1];

I'm sure it can be made simpler and better, but that might give you a start. 我相信它可以变得更简单,更好,但这可能会给你一个开始。

var str = "http://www.example.com/members/admin/projects/?projectid=41";
var newStr = "/ajax/" + str.split("/").slice(-2).join(".php");
console.log(newStr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM