正则expression式:在string'styles / customer.1031.css'中匹配'customer'
我试图从nodejs中的文件path中提取客户string。 到目前为止,我已经想出了这个:
var fileName = 'styles/customer.1031.css'; fileName = fileName.substring(7); fileName = fileName.substring(0, fileName.length - 4); fileName = fileName.match('[az]*')[0]; console.log(fileName); // <-- yields 'customer'
我从头开始剪裁styles/
,最后从.css
开始。 然后我只匹配小写字符。 什么是适当的正则expression式只匹配客户string,所以我不需要切断string? F. 正则expression式看起来像styles/
直到后面的所有东西.
?
使用正则expression式可能看起来像^styles/([^.]+)\..*$
其中
-
^styles/
翻译为“以'样式/'开头 - 然后你的匹配(至less有一个字符,直到第一个匹配)。
- 然后一个字面的'。'
- 然后任何东西,直到string的结尾(这是可选的,根据您的需要)
正则expression式看起来像样式/直到。
这是它的样子:
styles\/(.*?)\.
在Regex101上运行它
被捕获的string可以通过\1
访问。
您可以使用非捕获组正则expression式(?:styles\/)(.*?)\.
var fileName = 'styles/customer.1031.css'; console.log(/(?:styles\/)(.*?)\./.exec(fileName)[1])