我正在使用此(http://coursesweb.net/javascript/convert-bbcode-html-javascript_cs)作为解析BBCode的脚本。我已经扩展了它可以处理的BBCodes,但是当换行符紧跟在开始标记之后,例如

  [code]
     code....
  [/code]


如果代码为“内联”,则不会发生此问题
      [code]code.... [/ code]`

用于匹配这些标记中的内容的正则表达式是(.*?),我知道它不匹配换行符。我试过([^\r\n])匹配换行符,但这也没有用。

我想这是一个简单的问题,但是我对正则表达式的经验很少,所以对您的任何帮助将不胜感激

编辑:这是我正在使用的正则表达式的完整列表

  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]


更新:
已将正则表达式修复为以下问题,这对我来说适用于Chrome控制台:

/\[code\]([\s\S]*?)\[\/code\]/g.exec("[code]hello world \n[/code]")

关于javascript - 用Javascript解析BBCode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34662519/

10-12 14:57