Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:16:47+00:00 2026-05-27T19:16:47+00:00

I am attempting to format an entire column of a table by replacing the

  • 0

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”. This is working fine.

My question involves replacing instances of “%0”, “%1”, “%2”, etc within the input string with table values. %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.

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:

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

$('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”. (I replaced the col="1" above with col="\$1" and appended ” \$1″ to use the first matched regex found). I should also note that in addition to “\$1” i used “$1” and “\$1” with no luck.

$('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. Since the result is “undefined 2” I know the regex is matching, but the lookup is incorrect. However, I know the code in general is correct since hardcoding col="2" will work.

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. 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:16:47+00:00Added an answer on May 27, 2026 at 7:16 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to extract all instances of a particular format from a string:
I was attempting to open a raw data file (.asc) format with Notepad and
I'm attempting to use boost::format, where my formatting string is the HTML below. I
I am attempting to parse a string in Perl with the format: Messages pushed
I am attempting to display data an XML source, in a tree format, to
I have a date field stored in the format 16/08/2010 17:17 I am attempting
I'm attempting to format data so that my json code can read it properly.
I'm studying a security unit and I'm attempting to understand format string attacks. Could
I'm attempting to convert dates from one format to another: From e.g. October 29,
Attempting to use the data series from this example no longer passes the JSONLint

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.