简体   繁体   English

正则表达式每行仅匹配第一个字符串

[英]Regular expression match only the first string per line

I'm trying to achieve this: 我正在努力实现这一目标:

given these lines: 给定这些行:

<script src="/vendor/handlebars/handlebars.js" charset="utf-8"></script>
<script src="/vendor/underscore/underscore.js" charset="utf-8"></script>

I would like to match: 我想搭配:

/vendor/handlebars/handlebars.js
/vendor/underscore/underscore.js

but I've achieved only this result. 但是我只达到了这个结果。

"/vendor/handlebars/handlebars.js" "utf-8"
"/vendor/underscore/underscore.js" "utf-8"

Using this pattern : 使用此模式:

/".+?"/gm

Here you can try it live, and watch my result. 在这里,您可以尝试它,并观看我的结果。

Any thoughts? 有什么想法吗?

You could use /src="([^"]+)"/gm and then get first matching group for each match. 您可以使用/src="([^"]+)"/gm ,然后为每个匹配项获取第一个匹配组。

For example: 例如:

s = '<script src="/vendor/handlebars/handlebars.js" charset="utf-8"></script>';
(/src="([^"]+)"/gm).exec(s)[1];

returns: 收益:

/vendor/handlebars/handlebars.js

在开头,简短和简单的地方检查反斜杠怎么办

"\/.+?"

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

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