简体   繁体   English

jQuery字符串替换匹配的正则表达式

[英]jQuery string replace matched regex

I am attempting to format an entire column of a table by replacing the data in each cell with a string. 我试图通过用字符串替换每个单元格中的数据来格式化表的整个列。 Simply, if I enter "nargles" in the input field and click a format button at the top of a table column, the text in every cell in that column is replaced with "nargles". 简单地说,如果我在输入字段中输入“nargles”并单击表格列顶部的格式按钮,则该列中每个单元格中的文本将替换为“nargles”。 This is working fine. 这工作正常。

My question involves replacing instances of "%0", "%1", "%2", etc within the input string with table values. 我的问题涉及用表值替换输入字符串中的“%0”,“%1”,“%2”等实例。 %0 corresponds to the 0th column, %1 for the 1st column, %2 for 2nd, etc. Additionally, it must grab the column value for the current row that is being modified. %0对应于第0列,%1表示第1列,%2表示第2列,等等。此外,它必须获取正在修改的当前行的列值。

To clarify with an example, lets take the table: 为了澄清一个例子,让我们拿表:

1    cat     description
2    dog     description
3    fish    description

If I entered "Row %0 is for %1" for my input and executed it on the 3rd column, the result would be: 如果我为输入输入“Row%0 for%1”并在第3列上执行,结果将是:

1    cat     Row 1 is for cat
2    dog     Row 2 is for dog
3    fish    Row 3 is for fish

Hopefully that is a sufficient explanation =) 希望这是一个充分的解释=)

So, here is a sample within the example table: 所以,这是示例表中的示例:

<tr>
    <td></td>
    <td><button class="format" col="1">Format Col</button></td>
    <td><button class="format" col="2" >Format Col</button></td>
</tr>

<tr>
    <td><input type="text" col="0" row="0" value="0" size="1"></td>
    <td><input type="text" col="1" row="0" value="cat" /></td>
    <td><input type="text" col="2" row="0" value="description" /></td>
</tr>

...

and here is the code for the format button at the top of each column 这是每列顶部格式按钮的代码

$('td button.format').on('click', function () {

    // get formatter variables and column number
    string = $("#formatter").attr("value");
    column = $(this).attr("col");

    // regex to globally look for %d (where d is an integer)
    var re = new RegExp("\%(\\d+)", "g");

    $('td input[col="' + column + '"]').each(function (row) {
        // replace string here
    });
});

Currently, this code structure will capture the 1st column's values and works fine 目前,此代码结构将捕获第1列的值并正常工作

$('td input[col="' + column + '"]').each(function (i) {
    $(this).attr("value", string.replace(re, $('td input[col="1" row="' + i +'"]').attr('value')));
});

But doing something like this (with input, "%2") will result in "undefined 2". 但是做这样的事情(输入“%2”)将导致“未定义2”。 (I replaced the col="1" above with col="\\$1" and appended " \\$1" to use the first matched regex found). (I取代col="1"以上col="\\$1"和所附“\\ $ 1”使用中发现的第一个匹配正则表达式)。 I should also note that in addition to "\\$1" i used "$1" and "\\$1" with no luck. 我还应该注意,除了“\\ $ 1”之外,我使用了“$ 1”和“\\ $ 1”而没有运气。

$('td input[col="' + column + '"]').each(function (i) {
    $(this).attr("value", string.replace(re, $('td input[col="\$1" row="' + i +'"]').attr('value') + " \$1"));
});

My conclusion is that the time in which the regex match is substituted into the jquery lookup and the time when the lookup is performed is not correct. 我的结论是正则表达式匹配被替换为jquery查找的时间和执行查找的时间不正确。 Since the result is "undefined 2" I know the regex is matching, but the lookup is incorrect. 由于结果是“未定义2”我知道正则表达式匹配,但查找不正确。 However, I know the code in general is correct since hardcoding col="2" will work. 但是,我知道代码一般是正确的,因为硬编码col="2"将起作用。

Any thoughts on why this is running into trouble. 有关为何遇到麻烦的任何想法。 I want to say its a syntax issue, but maybe I am wrong. 我想说它是一个语法问题,但也许我错了。

Sidenote: This could all be avoided if I just used the re.match() function and iterated over the returned array. 旁注:如果我只使用re.match()函数并迭代返回的数组,则可以避免这一切。 I know a solution exists, im just seeing if there is a more elegant/cleaner way to do it. 我知道存在一个解决方案,我只是看看是否有更优雅/更清洁的方式来做到这一点。

I know this is long, sorry! 我知道这很长,抱歉! I figure more info is better than not enough. 我认为更多的信息比不够好。 Thanks for reading all the way through! 感谢您一路阅读!

I guess you're looking for something like this: 我想你正在寻找这样的东西:

 $('td button.format').click(function () {
     var template = "Row %0 is for %1";
     var col = $(this).attr("col");

     $("#body tr").each(function() {
         var $row = $(this);
         var t = template.replace(/%(\d+)/g, function($0, $1) {
             return $row.find("td:eq(" + $1 + ") input").val();
         });
         $row.find("td:eq(" + col + ") input").val(t)
     })
 })

Complete example: http://jsfiddle.net/TTjtU/ 完整示例: http//jsfiddle.net/TTjtU/

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

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