用于查找 中的属性的 Javascript 正则表达式是什么?标签?

发布于 2024-11-02 04:09:41 字数 54 浏览 1 评论 0原文

我有一个

。位于输入到

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安静被遗忘 2024-11-09 04:09:41

使用 DOM 函数。下面是一些使用 jQuery 的代码,使其更容易阅读:

$('#your-div span').each(function() {
    var elem = this, $elem = $(this);
    $.each(elem.attributes, function(i, attr) {
        if(attr) {
            $elem.removeAttr(attr.name);
        }
    });
});

演示: http://jsfiddle.net/ThiefMaster/ qfsAb/

但是,在您的情况下,您可能不仅希望从跨度中删除属性,而且还希望从所有元素中删除属性,除非该属性是例如 alignhref

因此,这里有一些 JS: http://jsfiddle.net/ThiefMaster/qfsAb/1/

$('#your-div').children().each(function() {
    var elem = this, $elem = $(this);
    $.each(elem.attributes, function(i, attr) {
        if(attr && attr.name != 'href' && attr.name != 'align') {
            $elem.removeAttr(attr.name);
        }
    });
});

Use DOM functions for it. Here's some code using jQuery to make it easier and nicer to read:

$('#your-div span').each(function() {
    var elem = this, $elem = $(this);
    $.each(elem.attributes, function(i, attr) {
        if(attr) {
            $elem.removeAttr(attr.name);
        }
    });
});

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 or href.

So, here's some JS for that: http://jsfiddle.net/ThiefMaster/qfsAb/1/

$('#your-div').children().each(function() {
    var elem = this, $elem = $(this);
    $.each(elem.attributes, function(i, attr) {
        if(attr && attr.name != 'href' && attr.name != 'align') {
            $elem.removeAttr(attr.name);
        }
    });
});
孤独岁月 2024-11-09 04:09:41

解析 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!

奶茶白久 2024-11-09 04:09:41

我在 http://jsfiddle.net/b3DSQ/ 做了一个工作演示

$('#editor span').each(function(i, el) {
    var attrs = el.attributes;
    $.each(attrs, function(i, a) {
        $(el).removeAttr(a.name);
    });
});

[我更改了复制的早期版本将内容写入内存,编辑,然后替换 - 没有意识到输入到 div 中的内容在 DOM 中自动有效]。

I made a working demo at http://jsfiddle.net/b3DSQ/

$('#editor span').each(function(i, el) {
    var attrs = el.attributes;
    $.each(attrs, function(i, a) {
        $(el).removeAttr(a.name);
    });
});

[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].

哎呦我呸! 2024-11-09 04:09:41

试试这个:

your_string.replace(/<span[^>]*>/g, '<span>');

但是,如果用户编写类似 的内容,则会中断

Try this:

your_string.replace(/<span[^>]*>/g, '<span>');

This will break however if the user writes something like <span title="Go > there">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文