使用JavaScript / jQuery将BBcode转换为HTML

我可以帮助将一些PHP代码转换为jQuery / JavaScript吗? 我想要的是一个简单的BBCode到HTML转换器。

这是PHP代码。 我想用jQuery / JavaScript实现同样的function。

$str = htmlentities($str); // The array of regex patterns to look for $format_search = array( '#\[b\](.*?)\[/b\]#is', '#\[i\](.*?)\[/i\]#is', '#\[u\](.*?)\[/u\]#is', ); // The matching array of strings to replace matches with $format_replace = array( '$1', '$1', '$1', ); // Perform the actual conversion $str = preg_replace($format_search, $format_replace, $str); 

谢谢你的帮助!

看起来几乎像你需要将#更改为/并且is ig但是我还必须将/b更改为\/b

现场演示

 $str = 'this is a [b]bolded[/b] and [i]italic[/i] string'; // The array of regex patterns to look for $format_search = [ /\[b\](.*?)\[\/b\]/ig, /\[i\](.*?)\[\/i\]/ig, /\[u\](.*?)\[\/u\]/ig ]; // note: NO comma after the last entry // The matching array of strings to replace matches with $format_replace = [ '$1', '$1', '$1' ]; // Perform the actual conversion for (var i =0;i<$format_search.length;i++) { $str = $str.replace($format_search[i], $format_replace[i]); } alert($str) 

其他现场演示

 function boldFunc(str, p1, offset, s) { return ''+encodeURIComponent(p1)+'' } function italicFunc(str, p1, offset, s) { return ''+encodeURIComponent(p1)+'' } function underlinedFunc(str, p1, offset, s) { return ''+encodeURIComponent(p1)+'' } $str = 'this is a [b]bölded[/b], [i]itälic[/i] and [u]ünderlined[/u] [i]strïng[/i]'; // The array of regex patterns to look for $format_search = [ /\[b\](.*?)\[\/b\]/ig, /\[i\](.*?)\[\/i\]/ig, /\[u\](.*?)\[\/u\]/ig ]; // NOTE: No comma after the last entry // The matching array of strings to replace matches with $format_replace = [ boldFunc, italicFunc, underlinedFunc ]; // Perform the actual conversion for (var i =0;i<$format_search.length;i++) { $str = $str.replace($format_search[i], $format_replace[i]); } document.getElementById('output').innerHTML=$str;