Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 转换'#ff00fffirstword#445533 secondword#008877 thirdword和#x27;转换为html标记格式_Javascript_Regex - Fatal编程技术网

Javascript 转换'#ff00fffirstword#445533 secondword#008877 thirdword和#x27;转换为html标记格式

Javascript 转换'#ff00fffirstword#445533 secondword#008877 thirdword和#x27;转换为html标记格式,javascript,regex,Javascript,Regex,我可以将字符串#ff00fffirstword#44553secondword#008877thirdword转换为 <font color='#ff00ff'>firstword</font><font color='#445533'>secondword</font><font color='#008877'>thirdword</font> firstwordsecondwordthirdword 在javascr

我可以将字符串
#ff00fffirstword#44553secondword#008877thirdword
转换为

<font color='#ff00ff'>firstword</font><font color='#445533'>secondword</font><font color='#008877'>thirdword</font>
firstwordsecondwordthirdword
在javascript或actionscript3程序中使用regexp

我尝试了下面的代码,但并不完美(actionscript3代码):

var-pat:RegExp=/(#\w{6})([^#]+)/g;
var html:String=t.replace(pat,“$2”);
跟踪(html);//输出:firstwordsecondwordthirdword

如果该字符串中还有一个
#
,则输出将与我希望的不同。我不知道如何编写更强大的regexp来实现这一点。

尝试使用
|
)运算符和对行尾的前瞻:

var pat:RegExp = /(#[0-9a-f]{6})([^#]+?)(?=#|$)/gi;
var html:String = t.replace(pat, "<font color=\'$1\'>$2</font>");
trace(html); 
查看输出

以及debuggex:

说明 你可以用这个正则表达式

正则表达式:
([#][0-9a-f]{6})(.*?(=[#][124;$)
替换为
\2

Javascript代码示例:

变量re=/([#][0-9a-f]{6})(*?(=[#][124;$)/;
var sourcestring=“#ff00fffirstword#44553secondword#008877thirdword”;
var replacementpattern=“\2”;
var result=sourcestring.replace(re,replacementpattern);
警报(“结果=”+结果);
替换后的$sourcestring:
第一个单词第二个单词第三个单词

您是如何生成正则表达式流程图的?sourcestring不应该是
#ff00fffirstword#44553secondword#008877thirdword
?@Jordan Trudgett&Rex Smith:是的,已经更正,请刷新浏览器。sourcestring='#ff00fffirstword#445533secondword#008877thirdword'@德鲁瓦塔克,克拉约拉!哈哈哈。但说真的,我正在使用debuggex.com。尽管它不支持lookbehind或原子组,但它仍然便于理解表达式流。还有regexper.com。它们也做得很好,但在你打字时不是实时的。顺便说一句,
font
tag已被弃用,不能保证将来能正常工作。最好使用CSS,例如,
如果源字符串是“#ff00fffirstword#44533secondword##008877thi#rdword#”,那么输出应该是firstwordsecondword#thi#rdword#@RexSmith添加了一个新的正则表达式来处理这个问题!我不知道你也有散列,所以现在应该可以了:)
var pat:RegExp = /(#[0-9a-f]{6})([^#]+?)(?=#|$)/gi;
var html:String = t.replace(pat, "<font color=\'$1\'>$2</font>");
trace(html); 
var pat:RegExp = /(#[0-9a-f]{6})(.+?)(?=#[0-9a-f]{6}|$)/gi;
var html:String = t.replace(pat, "<font color=\'$1\'>$2</font>");
trace(html); 
<script type="text/javascript">
  var re = /([#][0-9a-f]{6})(.*?)(?=[#]|$)/;
  var sourcestring = "#ff00fffirstword#445533secondword#008877thirdword";
  var replacementpattern = "<font color='\1'>\2</font>";
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>

$sourcestring after replacement:
<font color='#ff00ff'>firstword</font><font color='#445533'>secondword</font><font color='#008877'>thirdword</font>