JavaScript正则表达式:为每个字符插入span标记

时间:2011-02-21 04:44:10

标签: javascript regex

我手边有一个正则表达式任务,可以真正使用一些帮助。

说我有如下文字:

To Sherlock Holmes she is always <i>THE</i> woman.

我需要将每个字符都包含在span标记中,HTML标记除外。例如,上面的文字将是:

<span>T</span><span>o</span><span> </span><span>S</span><span>h</span>
<span>e</span><span>r</span><span>l</span><span>o</span><span>c</span>
<span>k</span><span> </span><span>H</span><span>o</span><span>l</span>
<span>m</span><span>e</span><span>s</span><span> </span><span>s</span>
<span>h</span><span>e</span><span> </span><span>i</span><span>s</span>
<span> </span><span>a</span><span>l</span><span>w</span><span>a</span>
<span>y</span><span>s</span><span> </span><i><span>T</span><span>H</span>
<span>E</span></i><span> </span><span>w</span><span>o</span><span>m</span>
<span>a</span><span>n</span><span>.</span>

请注意:

  • 每个字符都包含在一个范围内 标签,甚至空间
  • HTML标记,<i></i>不是

欢迎任何建议。

谢谢!

3 个答案:

答案 0 :(得分:4)

DOM交互可以更好地处理这项工作。以下两个实用程序函数将帮助使用span标记包装给定文本中的每个字符。

/**
 * recursively get all text nodes as an array for a given element
 */
function getTextNodes(node) {
    var childTextNodes = [];

    if (!node.hasChildNodes()) {
        return;
    }

    var childNodes = node.childNodes;
    for (var i = 0; i < childNodes.length; i++) {
        if (childNodes[i].nodeType == Node.TEXT_NODE) {
            childTextNodes.push(childNodes[i]);
        }
        else if (childNodes[i].nodeType == Node.ELEMENT_NODE) {
            Array.prototype.push.apply(childTextNodes, getTextNodes(childNodes[i]));
        }
    }

    return childTextNodes;
}

/**
 * given a text node, wrap each character in the
 * given tag.
 */
function wrapEachCharacter(textNode, tag) {
    var text = textNode.nodeValue;
    var parent = textNode.parentNode;

    var characters = text.split('');
    characters.forEach(function(character) {
        var element = document.createElement(tag);
        var characterNode = document.createTextNode(character);
        element.appendChild(characterNode);

        parent.insertBefore(element, textNode);
    });

    parent.removeChild(textNode);
}

现在给出一些HTML,我们将创建它的DOM表示,然后使用第一个函数getTextNodes从中检索所有文本节点。一旦我们拥有了所有文本节点,我们就可以将它们中的每一个传递给第二个函数 - wrapEachCharacter

// create a wrapper element that will hold our HTML.
var container = document.createElement('div');
container.innerHTML = "To Sherlock Holmes she is always <i>THE</i> woman.";

// get all text nodes recursively.
var allTextNodes = getTextNodes(container);

// wrap each character in each text node thus gathered.
allTextNodes.forEach(function(textNode) {
    wrapEachCharacter(textNode, 'span');
});

发布了一个示例here

答案 1 :(得分:3)

沿着这条线的东西应该可以做到这一点

txt = txt.replace (/(<.*?>)|(.)/g, function (m0, tag, ch) {
   return tag || ('<span>' + ch + '</span>');
});

答案 2 :(得分:1)

不要使用正则表达式,只需使用for循环遍历字符串:

var s = 'To Sherlock Holmes she is always <i>THE</i> woman.';
var out = '';
for (var z = 0; z < s.length; ++z) {
    var ch = s.charAt(z);
    if (ch == '<') {
        while (ch != '>') {
            out += ch;
            ch = s.charAt(++z);
        }
        out += ch;
        continue;
    }
    out += '<span>' + ch + '</span>';
}
alert(out);