Javascript <;br/>;及;nbsp;出现在搜索文本中
我有一个模式,用户可以通过Javascript <;br/>;及;nbsp;出现在搜索文本中,javascript,regex,angularjs,Javascript,Regex,Angularjs,我有一个模式,用户可以通过textarea添加注释。他们能够在文本区域中键入多个空格和换行符。添加注释后,它会立即出现在注释模式中。上面的输入字段允许用户搜索新创建的注释。突出显示范围将包裹在其上搜索的文本 我正在使用regex创建和,以便文本区域的格式显示在显示的注释中 问题是创建的和会出现在高亮显示的搜索结果中。 高光过滤器 .filter('highlighted',函数($sce){ 返回函数(注释、短语){ 如果(短语){ //短语=短语。替换(/^\ \|*/gi,“”)。替
textarea
添加注释。他们能够在文本区域中键入多个空格和换行符。添加注释后,它会立即出现在注释模式中。上面的输入字段允许用户搜索新创建的注释。突出显示范围将包裹在其上搜索的文本
我正在使用regex创建
和
,以便文本区域的格式显示在显示的注释中
问题是创建的
和
会出现在高亮显示的搜索结果中。
高光过滤器
.filter('highlighted',函数($sce){
返回函数(注释、短语){
如果(短语){
//短语=短语。替换(/^\ \|*/gi,“”)。替换()。修剪(“”);
note=note.replace(新的RegExp(“(“+phrase+”)”,“gim”),“$1”);
}
返回$sce.trustAsHtml(注);
};
})
票据服务
noteService.getNotes(idPart)
.成功(功能(结果){
$scope.notes=result.notes;
如果($scope.notes){
result.notes.forEach(函数addDates(notes)){
notes.dateSort=moment(notes.timestamp).format('MMMM D YYYY,h:mm:ss a');
notes.timestamp=时刻(notes.timestamp).format('mmmd,YYYY');
notes.notes_raw=notes.note;
变量托架=/\n/g,
空格=/\s/g,
findCarriage=notes.note.replace(回车“
”),
newstr=findCarriage.replace(空格“”);
notes.note=newstr;
});
}
console.log('success');
})
.错误(函数(){
$scope.$emit('alert','danger','Failed to retrieve notes');
控制台日志(“故障”);
$modalInstance.close($scope.$parent.$id);
});
HTML
-
添加
清楚的
尝试使用/|/gi
。谢谢,但它不起作用代码>您给出的尝试还删除了nbsp和br的格式,我想保留我提到的^\nbsp\|*`。它有几个问题,但我想这与这里描述的yoyur问题无关。是否有什么原因使您避免使用应该能够处理显示额外空格和换行符的标记?尝试使用/|/gi
。谢谢,但它不起作用短语=短语。替换(/|/gi,”).trim(“”)代码>您给出的尝试还删除了nbsp和br的格式,我想保留我提到的^\nbsp\|*`。它有几个问题,但我想这与这里描述的yoyur问题无关。是否有什么原因使您避免使用应该能够显示额外空格和换行符的标记?
.filter('highlighted', function($sce) {
return function (note, phrase) {
if (phrase) {
//phrase = phrase.replace(/^\ \;|<br?\>*/gi, "").replace().trim("");
note = note.replace(new RegExp('(' + phrase + ')', 'gim'), '<span class="highlighted">$1</span>');
}
return $sce.trustAsHtml(note);
};
})
noteService.getNotes(idPart)
.success(function(result) {
$scope.notes = result.notes;
if ($scope.notes) {
result.notes.forEach(function addDates(notes) {
notes.dateSort = moment(notes.timestamp).format('MMMM D YYYY, h:mm:ss a');
notes.timestamp = moment(notes.timestamp).format('MMM D, YYYY');
notes.notes_raw = notes.note;
var carriage = /\n/g,
space = /\s/g,
findCarriage = notes.note.replace(carriage, "<br/>"),
newstr = findCarriage.replace(space, " ");
notes.note = newstr;
});
}
console.log('success');
})
.error(function() {
$scope.$emit('alert', 'danger', 'Failed to retrieve notes');
console.log("failure");
$modalInstance.close($scope.$parent.$id);
});
<input type="text" class="form-control" ng-model="search.notes" placeholder="Search" aria-describedby="sizing-addon3" />
<ul class="notes-list">
<li ng-repeat="note in notes | filter:search.notes | orderBy:'-dateSort'" class="note well">
<span><strong ng-bind-html="note.timestamp | highlighted:search.notes"></strong> <em ng-bind-html="note.user | highlighted:search.notes"></em></span>
<p ng-bind-html="note.note | highlighted:search.notes"></p>
</li>
</ul>
<textarea autofocus class="form-control" rows="3" id="new-note" ng-model="data.note" maxlength="140" placeholder="Character limit 140 characters" required ng-focus="characterCount();"></textarea>
<button type="submit" class="btn btn-xs btn-info" ng-disabled="partNotes.$invalid" ng-click="add()
">Add</button>
<button type="submit" class="btn btn-xs btn-link" ng-click="clear()" formnovalidate>Clear</button>