regex - Parsing BBCode in Javascript -
i using (http://coursesweb.net/javascript/convert-bbcode-html-javascript_cs) script parsing bbcode. have extended bbcodes can process, encountering problem when newline follows opening tag, e.g.
[code] code.... [/code]
the problem not occur if code 'inline' [code]code....
[/code]`
the regex being used match what's inside these tags (.*?)
know not match newlines. have tried ([^\r\n])
match newlines hasn't worked either.
i imagine it's simple issue have little experience regex appreciated
edit: full list of regex's using
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])', };
edit 2: full js matching
var token_match = /{[a-z_]+[0-9]*}/ig; var _getregex = function(str) { var matches = str.match(token_match); var nrmatches = matches.length; var = 0; var replacement = ''; if (nrmatches <= 0) { return new regexp(preg_quote(str), 'g'); // no tokens return escaped string } for(; < nrmatches; += 1) { // remove {, } , numbers token can match // keys in tokens var token = matches[i].replace(/[{}0-9]/g, ''); if (tokens[token]) { // escape before token replacement += preg_quote(str.substr(0, str.indexof(matches[i]))) + tokens[token]; // remove before end of token can used // next token. doing parts can 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 = 0; var replacement = ''; var positions = {}; var next_position = 0; if (nrmatches <= 0) { return str; // no tokens return string } for(; < nrmatches; += 1) { // remove {, } , numbers token can match // keys in tokens var token = matches[i].replace(/[{}0-9]/g, ''); var position; // figure out $# use ($1, $2) if (positions[matches[i]]) { position = positions[matches[i]]; } else { // token doesn't have position increment next position // , record 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; };
this trick me: (updated 1 avoid confusion)
\[code\]((?:.|\t|\n|\r)*?)\[\/code\]
see regexpal , enter following:
[code] code.... [/code] [code]code.... [/code]
update: fixed regex following , works in chrome console me:
/\[code\]((?:.|\t|\n|\r)*?)\[\/code\]/g.exec("[code]hello world \n[/code]")
Comments
Post a Comment