简体   繁体   English

使用jQuery选择器和JavaScript正则表达式更改字符串

[英]Change a string using jquery selector and javascript regex

All I want to do is replace the letter "y" with the letter "i" in all s with a certain class. 我要做的就是在某个类中将所有s中的字母“ y”替换为字母“ i”。 I'm not sure why my script isn't working. 我不确定为什么我的脚本不起作用。 I know the selector is working because I can swap the method to something like hide() and it works. 我知道选择器正在工作,因为我可以将方法交换到诸如hide()之类的方法上并且可以工作。 I also can assign the value of the replace() into a variable, and that variable shows the correct substitution. 我还可以将replace()的值分配给一个变量,并且该变量显示正确的替换。 So I'm boggled why the combination of the two isn't working... 所以我很困惑为什么两者不能结合使用...

<script type="text/javascript" src=".../jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
    $("span.certain_class").text().replace(/y/g, "i");
}
);
</script>';

EDIT: I need to clarify that I need to do this to multiple spans throughout the page. 编辑:我需要澄清一下,我需要对整个页面的多个范围执行此操作。 Remember that the text() method stacks up all the texts of each span... so something like this won't work: 请记住,text()方法会堆叠每个跨度的所有文本...因此,这样的操作将不起作用:

$("span.certain_class").text($("span.certain_class").text().replace(...))

Each span will end up with a mess of the other span's text. 每个跨度最终都会导致其他跨度文本混乱不堪。

$(document).ready(function() {
    $('span.certain_class').each(function() {
        $(this).text($(this).text().replace(/y/ig, function(s) {
            return (s === 'Y' ? 'I' : 'i');
        }));
    });
});

A typo in first line! 第一行有错字!

<script type="text/javascript" src=".../jquery.min.js"></script>

should be 应该

<script type="text/javascript" src="../jquery.min.js"></script>

working demo! 工作演示!

or if you want to change it directly to the same span, use 或者如果您想直接将其更改为相同的跨度,请使用

    $("span.certain_class").text($("span.certain_class").text().replace(/y/g, "i")); 

http://sandbox.phpcode.eu/g/ab47c/2 http://sandbox.phpcode.eu/g/ab47c/2

  $("span.certain_class").text(
    $("span.certain_class").text().replace(/y/g, "i")
  );

Your script does correctly replace all the "y"'s with "i"'s but it doesn't assign the value back to the dom element. 您的脚本确实将所有的“ y”替换为“ i”,但是没有将值分配回dom元素。

The text() function only gets the contents. text()函数仅获取内容。 But with you're current piece of code you don't replace the contents. 但是,使用您当前的代码,您不会替换其内容。

var replacement = $("span.certain_class").text().replace(/y/g, "i");
$("span.certain_class").text(replacement);

Well, your solution works but you don't do anything with the result. 好吧,您的解决方案有效,但是您什么都不做。 Do you want to replace the text inside the div? 您要替换div中的文本吗? Try this: 尝试这个:

$(document).ready(function() {
        var elm = $("span.certain_class");
        var text = elm.text().replace(/y/g, "i"); // Get current text and replace y to i
        elm.text(text); // Set new text
    }
);

text() called without arguments only returns the element's inner text, and since strings are immutable in Javascript, your call to replace() will create a new string that's immediately forgotten. 不带参数调用的text()仅返回元素的内部文本,并且由于字符串在Javascript中是不可变的,因此您对replace()调用将创建一个立即被忘记的新字符串。

You can pass a function to text() in order to build the new value from the previous one: 您可以将一个函数传递给text() ,以便从上一个构建新值:

$("span.certain_class").text(function(index, currentText) {
    return currentText.replace(/y/g, "i");
});

It should be: 它应该是:

var replaceSpan = $("span.certain_class")
replaceSpan.text(replaceSpan.text().replace(/y/g, "i"));

You need to pass a string to the .text() method. 您需要将字符串传递给.text()方法。 All you're currently doing is grabbing the text of span.certain_class as a string ( .text() ), performing the replace ( .replace(..) ), but then not doing anything with the resulting string. 您当前要做的只是将span.certain_class的文本作为字符串( .text() )进行,执行replace( .replace(..) ),但是随后对所得字符串不做任何操作。

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

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