用于查找 中的属性的 Javascript 正则表达式是什么?标签?
我有一个
。位于输入到
I have a <div contenteditable="true>
that I am trying to use to get text from users. I would use a simple <textarea>
, but I need line-breaks and eventually lists and tables etc. I am trying to build something that is semi a rich-text editor, but not a full fledged one. I do not want to use a rich text editor.
I am trying to eliminate the attributes on <span>
tags from the text that is typed into the <div contenteditable="true>
. Is there a way to do that with Regexp? I was having difficulties coming up with a Regexp because I can't figure out how to make it so that the string starts with <span
and ends with >
and any number of characters can be in between. Is there a way to combine that in one Regexp? I came up with /^<span >$/
but that does not work because there is no division between the two strings. Would something like this work: /^[<span][>]$/g
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 DOM 函数。下面是一些使用 jQuery 的代码,使其更容易阅读:
演示: http://jsfiddle.net/ThiefMaster/ qfsAb/
但是,在您的情况下,您可能不仅希望从跨度中删除属性,而且还希望从所有元素中删除属性,除非该属性是例如
align
或href
。因此,这里有一些 JS: http://jsfiddle.net/ThiefMaster/qfsAb/1/
Use DOM functions for it. Here's some code using jQuery to make it easier and nicer to read:
Demo: http://jsfiddle.net/ThiefMaster/qfsAb/
However, in your case you might want to remove attributes not only from spans but from all elements unless the attribute is e.g.
align
orhref
.So, here's some JS for that: http://jsfiddle.net/ThiefMaster/qfsAb/1/
解析 HTML,然后删除属性。如果您在浏览器中执行此操作,则您可以使用高级 HTML 解析器(或者您有 IE),因此请使用它!
Parse the HTML and then strip out the attributes afterwards. If you're doing this in a browser, you have a high grade HTML parser right at your disposal (or you have IE), so use it!
我在 http://jsfiddle.net/b3DSQ/ 做了一个工作演示
[我更改了复制的早期版本将内容写入内存,编辑,然后替换 - 没有意识到输入到 div 中的内容在 DOM 中自动有效]。
I made a working demo at http://jsfiddle.net/b3DSQ/
[I changed an earlier version which copied the contents into memory, edited, and then replaced - hadn't realised that the stuff typed into the div was automatically valid in the DOM].
试试这个:
但是,如果用户编写类似
的内容,则会中断
Try this:
This will break however if the user writes something like
<span title="Go > there">