用Javascript解析bbc代码
我使用这个()作为解析BBCode的脚本。我已经扩展了它可以处理的BBCODE,但是当换行符紧跟在开始标记之后时,我遇到了一个问题,例如用Javascript解析bbc代码,javascript,regex,bbcode,Javascript,Regex,Bbcode,我使用这个()作为解析BBCode的脚本。我已经扩展了它可以处理的BBCODE,但是当换行符紧跟在开始标记之后时,我遇到了一个问题,例如 [code] code.... [/code] 如果代码是“内联”的,则不会出现问题 [code]code….[/code]` 用于匹配这些标记内部内容的正则表达式是(.*),我知道它与换行符不匹配。我尝试了([^\r\n])来匹配换行符,但这也不起作用 我想这是一个简单的问题,但我对regex几乎没有经验,所以如果有任何帮助,我将不胜感激
[code]
code....
[/code]
如果代码是“内联”的,则不会出现问题
[code]code….
[/code]`
用于匹配这些标记内部内容的正则表达式是(.*)
,我知道它与换行符不匹配。我尝试了([^\r\n])
来匹配换行符,但这也不起作用
我想这是一个简单的问题,但我对regex几乎没有经验,所以如果有任何帮助,我将不胜感激
编辑:这是我正在使用的正则表达式的完整列表
var tokens = {
'URL' : '((?:(?:[a-z][a-z\\d+\\-.]*:\\/{2}(?:(?:[a-z0-9\\-._~\\!$&\'*+,;=:@|]+|%[\\dA-F]{2})+|[0-9.]+|\\[[a-z0-9.]+:[a-z0-9.]+:[a-z0-9.:]+\\])(?::\\d*)?(?:\\/(?:[a-z0-9\\-._~\\!$&\'*+,;=:@|]+|%[\\dA-F]{2})*)*(?:\\?(?:[a-z0-9\\-._~\\!$&\'*+,;=:@\\/?|]+|%[\\dA-F]{2})*)?(?:#(?:[a-z0-9\\-._~\\!$&\'*+,;=:@\\/?|]+|%[\\dA-F]{2})*)?)|(?:www\\.(?:[a-z0-9\\-._~\\!$&\'*+,;=:@|]+|%[\\dA-F]{2})+(?::\\d*)?(?:\\/(?:[a-z0-9\\-._~\\!$&\'*+,;=:@|]+|%[\\dA-F]{2})*)*(?:\\?(?:[a-z0-9\\-._~\\!$&\'*+,;=:@\\/?|]+|%[\\dA-F]{2})*)?(?:#(?:[a-z0-9\\-._~\\!$&\'*+,;=:@\\/?|]+|%[\\dA-F]{2})*)?)))',
'LINK' : '([a-z0-9\-\./]+[^"\' ]*)',
'EMAIL' : '((?:[\\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*(?:[\\w\!\#$\%\'\*\+\-\/\=\?\^\`{\|\}\~]|&)+@(?:(?:(?:(?:(?:[a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(?:\\d{1,3}\.){3}\\d{1,3}(?:\:\\d{1,5})?))',
'TEXT' : '(.*?)',
'SIMPLETEXT' : '([a-zA-Z0-9-+.,_ ]+)',
'INTTEXT' : '([a-zA-Z0-9-+,_. ]+)',
'IDENTIFIER' : '([a-zA-Z0-9-_]+)',
'COLOR' : '([a-z]+|#[0-9abcdef]+)',
'NUMBER' : '([0-9]+)',
'ALL' : '([^\r\n])',
};
编辑2:匹配的完整JS
var token_match = /{[A-Z_]+[0-9]*}/ig;
var _getRegEx = function(str) {
var matches = str.match(token_match);
var nrmatches = matches.length;
var i = 0;
var replacement = '';
if (nrmatches <= 0) {
return new RegExp(preg_quote(str), 'g'); // no tokens so return the escaped string
}
for(; i < nrmatches; i += 1) {
// Remove {, } and numbers from the token so it can match the
// keys in tokens
var token = matches[i].replace(/[{}0-9]/g, '');
if (tokens[token]) {
// Escape everything before the token
replacement += preg_quote(str.substr(0, str.indexOf(matches[i]))) + tokens[token];
// Remove everything before the end of the token so it can be used
// with the next token. Doing this so that parts can be escaped
str = str.substr(str.indexOf(matches[i]) + matches[i].length);
}
}
replacement += preg_quote(str);
return new RegExp(replacement, 'gi');
};
var _getTpls = function(str) {
var matches = str.match(token_match);
var nrmatches = matches.length;
var i = 0;
var replacement = '';
var positions = {};
var next_position = 0;
if (nrmatches <= 0) {
return str; // no tokens so return the string
}
for(; i < nrmatches; i += 1) {
// Remove {, } and numbers from the token so it can match the
// keys in tokens
var token = matches[i].replace(/[{}0-9]/g, '');
var position;
// figure out what $# to use ($1, $2)
if (positions[matches[i]]) {
position = positions[matches[i]];
} else {
// token doesn't have a position so increment the next position
// and record this token's position
next_position += 1;
position = next_position;
positions[matches[i]] = position;
}
if (tokens[token]) {
replacement += str.substr(0, str.indexOf(matches[i])) + '$' + position;
str = str.substr(str.indexOf(matches[i]) + matches[i].length);
}
}
replacement += str;
return replacement;
};
var-token_-match=/{[A-Z]+[0-9]*}/ig;
var_getRegEx=函数(str){
var matches=str.match(标记匹配);
var nrrmatches=matches.length;
var i=0;
var替换=“”;
if(nrmatchesJavaScript不处理多行RegExp匹配。相反,您必须使用中描述的[\s\s]
技巧。也许
/\[code\][\s\S]*\[code\]/
此外,RegExps可能不是解析语法的最佳选择。它极其复杂。我建议解析字符串并构建一个抽象语法树,然后从中呈现HTML。这对我来说很有用:(也更新了这个以避免混淆)
请参阅并输入以下内容:
[code]
code....
[/code]
[code]code.... [/code]
更新:
将正则表达式修复为以下内容,这在Chrome控制台中对我有效:
/\[code\]([\s\S]*?)\[\/code\]/g.exec("[code]hello world \n[/code]")
你能提供完整的正则表达式吗?另外:你的内联[code]中有一个输入错误。你缺少结束标记前的结束斜杠。@Freddy我现在已经更新了问题,谢谢,请看我的答案。这对我有效。这在regexpal中有效,但只显示[code]之间的最后一个字符当我实现itI时,我在与字符串匹配的函数中添加了标记虽然这在regexpal中非常有效,但在我实现它时没有任何效果
/\[code\]([\s\S]*?)\[\/code\]/g.exec("[code]hello world \n[/code]")