简体   繁体   English

更换问题

[英]Replacement issues

I've been having issues with replacements and javascript for the past week. 过去一周,我一直在更换和javascript方面遇到问题。 I'm doing all sort of replacements with Python and so far I've never had so many issues. 我正在用Python进行各种替换,到目前为止,我从未遇到过太多问题。 Please help me? 请帮我? :P :P

This is the part of my code that gives me problems: 这是我的代码中给我带来问题的部分:

function SaeReplacerMenuButton(buttonchosen){
var Texto = document.getElementById("wpTextbox1").value;
$("."+buttonchosen).each(function() {
    var Reemplazo = this.value.split("=");
    var PalabraAntigua = new RegExp(Reemplazo[0],'g'); /*Esto genera el regext*/
    Texto = Texto.replace(" "+PalabraAntigua+" ", " "+Reemplazo[1]+" ");
    Texto = Texto.replace(" "+PalabraAntigua+".", " "+Reemplazo[1]+".");
    Texto = Texto.replace(" "+PalabraAntigua+",", " "+Reemplazo[1]+",");
    }
);
document.getElementById("wpTextbox1").value = Texto;
}

It should: 这应该:

  1. Get a string from specific fields (textareas with an specific class, actually). 从特定字段(实际上是具有特定类的文本区域)获取字符串。 The value in one of those textareas is as example: "miedo={{miedo}}". 这些文本区域之一中的值例如:“ miedo = {{{miedo}}”。
  2. Then split it into 2. 然后将其拆分为2。
  3. Then do a search for the first split between whitespaces " miedo ". 然后搜索空白“ miedo”之间的第一个分隔。
  4. I finds any match, replace it for the 2nd split ( {{miedo}} ). 我找到任何匹配项,将其替换为第二个拆分({{miedo}})。

After that, I added some code to make replacementes if also finds it with a , and a . 之后,如果还找到了,和,我添加了一些代码进行替换。 instead of just whitespaces, and that's when the problems starts. 而不仅仅是空白,那就是问题开始的时候。 When I run the script, it replaces: 当我运行脚本时,它将替换为:

miedo miedo miedo miedo miedo miedo miedo miedo miedo miedo

with: 有:

 miedo {{miedo}} {{miedo}}.{{miedo}} {{miedo}}.{{miedo}} {{miedo}}.{{miedo}} {{miedo}}.{{miedo}} miedo

But why? 但是为什么呢? there wasn't any dot in the original text, so the code that should replace it adding a dot shouldn't do anything. 原始文本中没有任何点,因此应替换为该代码并添加点的代码不会执行任何操作。

Note: I know nothing about RegEx, so this was an attempt to do a global replacement with a var + whitespaces instead of just a string. 注意:我对RegEx一无所知,因此这是尝试用var +空格而不是仅字符串来进行全局替换。 Is there any "easy" way to do that? 有什么“简单”的方法可以做到吗?

I tried to replicate it on jsfiddle as seen at http://prntscr.com/7wwns1 but I was unable to make it works, so I decided to upload the script so you guys can test it. 我尝试在jsfiddle上复制它,如http://prntscr.com/7wwns1所示,但是我无法使其正常工作,因此我决定上载脚本,以便大家对其进行测试。
It's a .rar file with a .js and a .html inside: http://coznothingisforever.webs.com/replacerforstackoverflow.rar 这是一个.rar文件,其中包含.js和.html: http : //coznothingisforever.webs.com/replacerforstackoverflow.rar

I think what you want for your function is something like this: 我认为您想要的功能是这样的:

function SaeReplacerMenuButton(buttonchosen){
    var Texto = document.getElementById("wpTextbox1").value;
    $("."+buttonchosen).each(function() {
        var Reemplazo = this.value.split("=");
        Texto = Texto.replace(new RegExp("(\\b)"+Reemplazo[0]+"(\\b)", "g"), "$1"+Reemplazo[1]+"$2");
    });
    document.getElementById("wpTextbox1").value = Texto;
}

If you really only want to match space, comma, and period after, the RegExp can be: 如果您只想匹配空格,逗号和句点,则RegExp可以是:

new RegExp("(\\b)"+Reemplazo[0]+"([ ,\\.])", "g")

When you create a RegExp object , and then add a sring to it, you get a string, not a regex. 当您创建RegExp对象时 ,然后向其添加一个sring,您将得到一个字符串,而不是正则表达式。

Have a look at what console writes: 看一下控制台的内容:

 var rx = new RegExp("word","g"); console.log(typeof(rx)); // -> object var rx2 = " " + rx + " "; console.log(typeof(rx2)); // -> string 

So, you cannot add spaces or any other strings like that. 因此,您不能添加空格或任何其他类似的字符串。 You need to add them right at the RegExp creation. 您需要在RegExp创建时直接添加它们。

Now, what you are looking for is word boundaries . 现在,您正在寻找的是单词边界

There are three different positions that qualify as word boundaries: 有三个不同的位置可作为单词边界:

Before the first character in the string, if the first character is a word character. 如果字符串中的第一个字符是单词字符,则在字符串中第一个字符之前。

After the last character in the string, if the last character is a word character. 如果字符串中的最后一个字符是单词字符,则在字符串的最后一个字符之后。

Between two characters in the string, where one is a word character and the other is not a word character. 字符串中的两个字符之间,其中一个是单词字符,另一个不是单词字符。

Word boundaries should not be placed in capturing groups as they are zero-width assertions that act like look-arounds, not consuming any text and are always empty (thus, no additional overhead with cpaturing groups or backreferences is necessary): 字边界不应放在捕获组中,因为它们是零宽度的断言,其作用类似于环顾四周,不占用任何文本,并且始终为空(因此,不需要使用捕获组或反向引用的额外开销):

 function replace() { var Texto = document.getElementById("wpTextbox1").value; var Reemplazo = "miédo={{miédo}}".split("="); var PalabraAntigua = new RegExp("\\\\b"+Reemplazo[0]+"\\\\b(?!}})",'g'); Texto = Texto.replace(PalabraAntigua, Reemplazo[1]); //var PalabraAntigua = new RegExp(Reemplazo[0],'g'); /*Esto genera el regext*/ //Texto = Texto.replace(" "+PalabraAntigua+" ", " "+Reemplazo[1]+" "); //Texto = Texto.replace(" "+PalabraAntigua+".", " "+Reemplazo[1]+"."); //Texto = Texto.replace(" "+PalabraAntigua+",", " "+Reemplazo[1]+","); document.getElementById("t").innerHTML = Texto; } 
 <input id="wpTextbox1" value="miédo miédo miédo miédo miédo miédo miédo miédo miédo miédo"/> <input type="Submit" onclick="replace();"/> <div id="t"/> 

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

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