在我的网络应用程序上,我查看当前 URL,如果当前 URL 是这样的形式:
http://www.domain.com:11000/invite/abcde16989/root/index.html
-> 我只需要将由 5 个字母和 5 个数字组成的 ID (abcde16989) 提取到另一个变量中以供进一步使用。
所以我需要这个:
var current_url = "the whole path, not just the hostname";
if (current_url has ID)
var ID = abcde16989;
最佳答案
如果 ID 始终位于同一位置,则您始终可以使用 split
使用 /
作为分隔符,例如
var parts = current_url.split('/');
var id = parts[4];
尽管您匹配“5 个字母和 5 个数字”的要求确实适合正则表达式匹配。
var id = current_url.match(/[a-zA-Z]{5}[0-9]{5}/); // returns null if not found
关于javascript - 请从此字符串中提取一些信息(没有正则表达式,以便我可以理解),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4042644/