【发布时间】:2015-10-27 14:46:41
【问题描述】:
当我要换行并从该行的开头开始标签时,荧光笔会将颜色应用于前一行。
<style>
#input{
color: transparent;
}
#input b{
text-decoration: none;
color: transparent;
background-color: #9aceff;
font-weight: normal;
}
#t{
background-color: transparent;
}
</style>
<textarea id="t" style="width: 342px; height: 92px; resize: none; position: absolute; z-index: 1; border: 1px solid #212;font-family: arial; padding: 20px; margin: 0; font-size: 12px;">
</textarea>
<div style="width: 300px; height: 50px; border: 1px solid #212; font-family: arial; padding: 20px; position: absolute; z-index: 0; margin: 0; font-size: 12px;" id="input">
</div>
<script>
$(document).ready(function(){
$('#t').on('input keyup', function() {
var str = $(this).val();
str = str.replace(/(<.+?>)/gi, '');
str = str.replace(/(?:\s|^)#([^0-9\W\s][a-zA-z0-9]*)/g, ' <b>#$1</b>');
str = str.replace(/(?:\r\n|\n\r|\r|\n)/g, '<br />');
$('#input').html(str);
});
});
</script>
这是我的 jsfiddle code。
【问题讨论】:
-
问题可能出在您的正则表达式上。为了清楚起见,我已将标签添加到问题中并编辑了问题。
-
是的,我知道,但我想不通,感谢您的完美编辑。
标签: javascript regex