[英]regex - capture all underscores between two strings
In this string(this is not my code, just sample input) 在这个字符串中(这不是我的代码,只是示例输入)
var b_1 = goog.require('a_b_b');
console.log(b_1.b);
a_b_b
should be renamed to abb
, so all underscores inside a goog.require('...')
statement should be renamed to a .
a_b_b
应该重命名为abb
,因此goog.require('...')
语句中的所有下划线都应重命名为a .
. 。
I came up with this regex: 我想出了这个正则表达式:
/goog\.require\('(?:[^_]*(_)[^_]*)*'\)/g
Explanation: 说明:
goog\.require\(' literal
(?: non-capturing group
[^_]* match anything except underscore
(_) capture underscore
[^_]* match anything except underscore
) end of non-capturing group
* there can be more than one underscore in a goog.require statement
'\) literal
But this only captures the last underscore. 但这仅捕获了最后一个下划线。 How can I capture all underscores in a goog.require('...')
statement? 如何在goog.require('...')
语句中捕获所有下划线?
I don't know if it's useful, but I'm replacing the underscores with javascript, so look behinds are not (natively) supported. 我不知道它是否有用,但是我用JavaScript代替了下划线,因此(本机)不支持回顾。
To be clear: I only want the underscores inside the goog.require('...')
statement to be replaced, so the underscore in b_1
should not be replaced. 需要明确的是:我只希望goog.require('...')
语句中的下划线, b_1
不应替换b_1
的下划线。
You can use this regex: 您可以使用此正则表达式:
var result = str.replace(/goog\.require\('[^']+'\)/g, function (match) {
return match.replace(/_/g, '.');
});
This first finds all occurrences that match the goog.require('str')
form and replaces all occurrences of '_'
with '.'
这首先查找与goog.require('str')
格式匹配的所有匹配项,然后将所有'_'
替换为'.'
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.