正在分析Javascript中的BBCode
Parsing BBCode in Javascript
我正在使用(http://coursesweb.net/javascript/convert-bbcode-html-javascript_cs)作为我解析BBCode的脚本。我已经扩展了它可以处理的BBCodes,但是当换行符紧跟在打开的标签后面时,我遇到了一个问题,例如
[code]
code....
[/code]
如果代码为"inline",则不会出现问题[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;
};
这对我来说很有用:(为了避免混淆,也更新了这个)
'[code'](['s'S]*?)'['/code']
请参阅regexpal并输入以下内容:
[code]
code....
[/code]
[code]code.... [/code]
更新:修复了以下regex,这在Chrome控制台中对我有效:
/'[code'](['s'S]*?)'['/code']/g.exec("[code]hello world 'n[/code]")
JavaScript不处理多行RegExp匹配。相反,您必须使用本SO答案中描述的['s'S]
技巧。可能
/'[code']['s'S]*'[code']/
此外,RegExps可能不是解析语法的最佳选择。它极其复杂。我建议解析字符串并构建一个抽象语法树,然后从中呈现HTML。
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 可以't让我的if语句处理js中的html表单输入
- 引用对象中的通用值
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 不能从angular2中的子组件指定父组件中的数组
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如何更改bigquery API中的计费层选项
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- BBcode编辑器中的新行
- 正在分析Javascript中的BBCode
- 只允许使用所见即所得BBCode编辑器中的html标记