从变量中提取元素 [英] Extracting elements from a variable

查看:127
本文介绍了从变量中提取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串' http://this.is.my .url:007/directory1/directory2/index.html ",我需要按如下所示提取字符串.请提出最好的方法

I have a string 'http://this.is.my.url:007/directory1/directory2/index.html' and I need to extract the string as below. Please advice the best way

var one = http://this.is.my.url:007 /directory1/directory2/index

var one = http://this.is.my.url:007/directory1/directory2/index

推荐答案

尝试一下:

var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.[^.]*$/g, ''); // would replace all file extensions at the end.

// or in case you only want to remove .html, do this:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.html$/g, '');

$字符(包含在正则表达式中)与文本字符串的末尾匹配.在变体a中,您从."开始.并删除此字符中的所有内容,直到字符串的末尾.在变体2中,将其减少为确切的字符串".html".这更多的是关于正则表达式,而不是javascript.要了解更多信息,请参阅以下许多不错的教程.

The $ character when included in a regular expression matches to the end of the text string. In variant a you look start at the "." and remove everything from the this character until the end of the string. In variant 2, you reduce this to the exact string ".html". This is more about regular expressions than about javascript. To learn more about it, here is one of many nice tutorials.

这篇关于从变量中提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆