Javascript 匹配两个给定标记内的所有文本
假设我有两个自定义javascript注释 开始:Javascript 匹配两个给定标记内的所有文本,javascript,regex,replace,Javascript,Regex,Replace,假设我有两个自定义javascript注释 开始://结束:/!> 我想替换这些标记之间的所有内容,还有标记 //<!- directiveDecoratorComment (function directiveDecorator() { if (typeof(angular) == 'undefined') return; var appElement = document.querySelector('[ng-app]'); va
//结束:/!>代码>
我想替换这些标记之间的所有内容,还有标记
//<!- directiveDecoratorComment
(function directiveDecorator() {
if (typeof(angular) == 'undefined') return;
var appElement = document.querySelector('[ng-app]');
var appName = appElement.getAttribute('ng-app');
if (typeof(appName) == 'undefined') return;
var app = angular.module(appName);
angular.forEach(app._invokeQueue, function(value, key) {
if (value[1] == 'directive') {
var directiveName = value[2][0];
app.config(function($provide) {
$provide.decorator(directiveName + 'Directive', function($delegate) {
var directive = $delegate[0];
console.log("Decorating:",directiveName,'on',appName,'template now ==',"/views/" + directive.templateUrl);
directive.templateUrl = "/views/" + directive.templateUrl;
return $delegate;
});
});
}
});
}());
//!>
//
因此,这将本质上成为一个空字符串javascript文件
这是我试过的
string.replace(/([//<])(.+\n*?)([//!>])/g, '');
string.replace(/([/])/g';
您想要的是:
string.replace(/(\/\/\<\!\-)((.*\n)*.*)(\/\/\!\>)/, '')
string.replace(/(\/\/\)/,“”)
您可以使用
/(\/\/<!-)([^\/]*(?:\/(?!\/!>)[^\/]*)*)(\/\/!>)/g
/(\/\/)[^\/]*)*(\/\/!>)/g
看
正则表达式基于展开循环技术
(\/\/-匹配并捕获文字//
([^\/]*(?:\/(?!\/!>)[^\/]*)
-匹配并捕获除/!>
以外的任何内容(在此处展开循环意味着我们将/
与[^\/]匹配任何字符)*
,然后匹配零组或多组未跟在/!>
后面的/
,然后再次匹配除/
之外的任意数量的字符)
(\/\/!>)
-匹配并捕获/!>
请注意,如果不使用捕获组,则可能会删除它们。您可以使用非贪婪匹配,无需提前查看即可完成此操作:
/\/\/<!-[\s\S]*?\/\/!>/
^^^^^^^ MATCH OPENING //<!-
^^^^^^^ ANYTHING IN BETWEEN
^^^^^^^ CLOSING //!>
/\/\//
^^^^^^^开赛//
\s\s
用于匹配换行符。类似于?使用,这似乎有效:str.replace(//\//\//g',);
请仔细阅读字符类的含义([]
)在JavaScript regexps中。您可以从这里开始:。这一个更快:。@WiktorStribiżew Yours是唯一一个真正为我使用JavaScript替换方法的人。谢谢!我会发布它:)