这里是数据列表。如何使用RegExp获取特殊字符串?
file:///E:/in/aaaaaaa
file:///E:/in/bbacc
file:///E:/in/ddafc
...
我希望在使用RegExp的“/ in”字之后得到类似aaaaaaa,bbacc,ddafc的字符串。
这里是数据列表。如何使用RegExp获取特殊字符串?
file:///E:/in/aaaaaaa
file:///E:/in/bbacc
file:///E:/in/ddafc
...
我希望在使用RegExp的“/ in”字之后得到类似aaaaaaa,bbacc,ddafc的字符串。
在Javascript中这样
var str = "one thing\\\'s for certain: power blackouts and surges can damage your equipment.";
alert(str.replace(/\\/g, ''))
var str = file_path;
alert(str.replace('file:\/\/\/E:\/in\/', ''))
我假设你想使用的JavaScript。
你可以使用像regex101这样的在线工具来处理你的正则表达式,它很方便,因为它可以捕捉你的语法错误,并解释正则表达式的每个部分在做什么。我觉得它非常有用。
下面是一些JavaScript,说明如何搭配你的字符串,并提取部分,你需要
var pattern = /^.*in\/(\w+)$/; // create a regular expression pattern, note that we include a capturing group (\w+) for the part of the string we wish to extract
var regexp = new RegExp(pattern); // create a RegExp object from your pattern
var filePath = "file:///E:/in/aaaaaaa"; // this is the string we will try to match
if(regexp.test(filePath) == true) { // if the pattern matches the string
var matches = regexp.exec(filePath); // call exec() to receive an object with the matches
console.log(matches[1]); // matches[1] contains what the first capturing group extracted
}
我建议用正则表达式玩弄有点明白了他们,他们是在第一相当复杂,它需要一些实践。这里有另一个网站,你可能会发现有用的:http://www.regular-expressions.info/
虽然这段代码可以解决问题,[包括解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)真的有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性注释来挤占代码,这会降低代码和解释的可读性! – kayess