简体   繁体   English

在<span>标签中</span>查找属性的Javascript Regexp是什么<span>?</span>

[英]What is the Javascript Regexp for finding the attributes in a <span> tags?

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. 我有一个<div contenteditable="true> ,我试图用它来从用户那里获取文本,我会使用一个简单的<textarea> ,但是我需要换行符,最后需要列表和表格等。那是半文本格式的文本编辑器,但不是完整的文本格式的编辑器。我不想使用文本格式的文本编辑器。

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 ? 我正在尝试从键入<div contenteditable="true>的文本中消除<span>标记上的属性。使用Regexp有办法吗?我遇到了一个Regexp的困难,因为我可以没有弄清楚如何使字符串以<span开头并以>结束并且可以在任意数量的字符之间插入。是否有一种方法可以将其组合到一个Regexp中呢?我想出了/^<span >$/但这不起作用,因为两个字符串之间没有除法,这样的工作是否可行: /^[<span][>]$/g

Parse the HTML and then strip out the attributes afterwards. 解析HTML ,然后去除属性。 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! 如果您在浏览器中执行此操作,则可以使用一个高级HTML解析器(或者您拥有IE) ,所以请使用它!

Use DOM functions for it. 为此使用DOM函数。 Here's some code using jQuery to make it easier and nicer to read: 这是一些使用jQuery使其更容易阅读的代码:

$('#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/ 演示: 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 eg align or href . 但是,在您的情况下,您可能不仅要从范围中删除属性,而且要从所有元素中删除属性,除非该属性是alignhref

So, here's some JS for that: http://jsfiddle.net/ThiefMaster/qfsAb/1/ 因此,这里有一些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);
        }
    });
});

I made a working demo at http://jsfiddle.net/b3DSQ/ 我在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]. [我更改了一个较早的版本,将内容复制到内存中,进行了编辑,然后进行了替换-尚未意识到键入div的内容在DOM中是自动有效的]。

Try this: 尝试这个:

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

This will break however if the user writes something like <span title="Go > there"> 但是,如果用户在<span title="Go > there">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM