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 8013237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:35:11+00:00 2026-06-04T19:35:11+00:00

I have the following text: var text= The sad sad man uses a bat

  • 0

I have the following text:

var text= 
    "The sad sad man uses a bat to swing the bats 
    away from his sad garden .
    Sadly he doesn't succeed. "

Let’s say i want to search for the word "sad".

var match;
re = /sad/g,
    match;
while (match = re.exec(text)) {
    console.log(match); 
match.poz = ....
}

How can i make match.poz to be a tuple(array) like this [line,position on the collumn] all starting from 0,0 ?

Eg.

  • 1 match –> match.poz = [0,4]
  • 2 match –> match.poz = [0,8]
  • 3 match –> match.poz = [1,14]
  • 4 match –> match.poz = [2,0]
  • 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-06-04T19:35:13+00:00Added an answer on June 4, 2026 at 7:35 pm

    I was able to build a simple parser, instead of using a regex, which I don’t think is possible (without a lot of help) to get the position in Javascript. All this does is go through the line, one character at a time, and “peek” ahead to see if the current position gives either sad or \n.

    var text = "The sad sad man uses a bat to swing the bats \naway from his sad garden .\nSadly he doesn't succeed.",
        length = text.length,
        matches = [],
        lines = 0,
        pos = 0;
    
    for (var i = 0; i < length; i++){
        var word = text.substring(i, i + 3).toLowerCase();
    
        if (word == 'sad') {
            matches[matches.length] = [lines, pos];
        }
    
        if (word.indexOf('\n') == 0) {
            lines++;
            pos = 0;
        } else {
            pos++;
        }
    }
    
    console.log(matches);
    

    Which gives me the following in Firebug console:

    [[0, 4], [0, 8], [1, 14], [2, 0]]
    

    http://jsfiddle.net/Zx5CK/1/

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

Sidebar

Related Questions

I have the following function: if ($(this).find('#sel').length == 0) { var before = $(this).text();
I have following code (live: http://jsfiddle.net/xyZY8/1/ ): var str = '<div>hello</div><ul><li>Some text...</li>' + '<li>second
I have following datas: var formFeilds={ 'text': { 'T1':{'required':true,'min':25,'max':55}, 'T2':{'required':true,'min':2,'max':5} } ,'text2':5 }; function
i have the following code, that randomizes text from an array. However what im
I have following code... <script type=text/javascript> var arr= new Array('10'); arr[0]='Need For Guru'; arr[1]='Qualities
everyone. I have following example script: <script type=text/javascript> $(document).ready(function() { var url = http://api.twitter.com/1/statuses/user_timeline/codinghorror.json;
I have to match the following text: var html = '<td align=right width=26%> <span
I have the following code: $(#tag-add-button).click(function () { var text = $(#tagadd).val(); var tagcounter=0;
I have the following code: var set = paper.set(); var text = paper.text(0, 0,
So I have following string: var s = '<span>Some Text</span> Some other Text'; The

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.