简体   繁体   English

javascript在特定字符之前删除字符串

[英]javascript remove string before a specific character

I've been looking and looking but can only find variations of the question I have... 我一直在寻找和寻找,但只能找到我的问题的变化......

I'm needing to remove a string before a character (including the character, and the spaces before and after the character). 我需要在字符前删除一个字符串(包括字符,以及字符前后的空格)。 The string will be different each time and unpredictable, so no matter what it is it needs to be removed from the DOM. 字符串每次都会有所不同,并且不可预测,因此无论它是什么,都需要从DOM中删除。 So, if this is the output: 所以,如果这是输出:

<h2>Birds - Norwegian Yellow Finch</h2>
<h2>Cats - Domestic Shorthaired Tabby</h2>
<h2>Dogs - Alaskan Husky Cross</h2>

How do I remove "Birds - , Cats - , Dogs - "? 如何删除“鸟 - 猫,狗 - 狗”? And without being specific about those words (Birds, Cats, Dogs) because those will change. 并没有具体说明那些词(鸟类,猫,狗)因为那些会改变。 The dash (-) can be a different character, if needed (ie: *). 如果需要,短划线( - )可以是不同的字符(即:*)。

Here is a solution without using a regular expression http://jsfiddle.net/46G5c/ : 这是一个不使用正则表达式http://jsfiddle.net/46G5c/的解决方案:

$('h2').each(function() {
    var h2 = $(this);
    var text = h2.text();
    var replacement = text.substr(text.indexOf('- ') + 2);
    h2.text(replacement);
});​

And here is one that uses a regular expression which may or may not be more useful depending on your data http://jsfiddle.net/X3FTV/1/ : 这里有一个使用正则表达式,根据您的数据http://jsfiddle.net/X3FTV/1/可能会或可能不会更有用:

var regex = /^[^-]* - /;
$('h2').each(function() {
    var h2 = $(this);
    var text = h2.text();
    h2.text(text.replace(regex, ''));
});​

Both use jQuery but should be straight forward to change to plain JavaScript. 两者都使用jQuery,但应该直接转换为纯JavaScript。

You can use a simple RegExp to solve this. 您可以使用简单的RegExp来解决此问题。

HTML: HTML:

<h2>Birds - Norwegian Yellow Finch</h2>
<h2>Cats - Domestic Shorthaired Tabby</h2>
<h2>Dogs - Alaskan Husky Cross</h2>​

JavaScript: JavaScript的:

var re = /^[^-]+-\s*/;

function processText(el) {
    el.textContent = el.textContent.replace(re, '');
}

var elements = ​document.getElementsByTagName('h2')​​​​;

for(var i = 0, l = elements.length; i < l; i++) {
    processText(elements[i]);
}​

DEMO DEMO

The following loops through all h2 elements and removes the beginning via a regex replace: 以下循环遍历所有h2元素并通过正则表达式替换删除开头:

window.onload = function() {
    var h2s = document.getElementsByTagName("h2"),
        i;

    for (i=0; i < h2s.length; i++)
        h2s[i].innerHTML = h2s[i].innerHTML.replace(/^[^-]+ - /,"");​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
};

Demo: http://jsfiddle.net/nGfYd/5/ 演示: http//jsfiddle.net/nGfYd/5/

The onload handler ensures that the code runs after the page has finished loading, because JavaScript is executed as it is encountered while the page is being parsed, but it can only manipulate elements that have been already been parsed. onload处理程序确保代码在页面加载完成后运行,因为JavaScript在解析页面时遇到,但它只能操作已经解析过的元素。 You don't need the onload handler if you include the code in a script block after the elements in question, in fact my preference is to put the <script> block at the end of the body just before the closing </body> tag and not use an onload handler. 如果在相关元素后面的脚本块中包含代码,则不需要onload处理程序,实际上我的首选是将<script>块放在正文结尾的</body>标记之前而不是使用onload处理程序。

EDIT: From comments under other answers it seems you are happy to use jQuery, in which case you could do this: 编辑:从其他答案的评论看来,你似乎很乐意使用jQuery,在这种情况下你可以这样做:

$(document).ready(function(){
    $("h2").text(function(i, oldVal) {
        return oldVal.replace(/^[^-]+ - /,"");
    });
});

Demo: http://jsfiddle.net/nGfYd/7/ 演示: http//jsfiddle.net/nGfYd/7/

If you pass a function to .text() jQuery will call that function for each element, passing it the old (current) value of the element, so you don't need an .each() loop too. 如果将函数传递给.text() jQuery将为每个元素调用该函数,并将元素的旧(当前)值传递给它,因此您也不需要.each()循环。

(Again the document ready handler is not needed if you put the code in a script block at the end of the body.) (如果将代码放在正文末尾的脚本块中,则不再需要文档就绪处理程序。)

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

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