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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:31:30+00:00 2026-05-27T21:31:30+00:00

What I’m trying to do is find all matches in a string, then wrap

  • 0

What I’m trying to do is find all matches in a string, then wrap them in a styled span tag. The solution I’ve come up with uses recursion, here’s the code:

function foo(node, pattern) {
    // alert(node.nodeValue);
    // alert(node.nodeValue + '\n' + pattern.test(node.nodeValue));
    if (pattern.test(node.nodeValue)) {
        // alert(node.nodeValue);
        var span = document.createElement('span');
        var text = document.createTextNode(RegExp.rightContext);

        span.className = 'someClass';
        span.innerHTML = RegExp.$1;

        node.nodeValue = RegExp.leftContext;
        node.parentNode.insertBefore(span, node.nextSibling);
        node.parentNode.insertBefore(text, span.nextSibling);

        foo(text, pattern);
    }

    return;
}

See it in action at this fiddle: http://jsfiddle.net/Umcaf/

The issue I’m having is that it will not mark out all instances of the word ‘test’. If you play around with the alert statements, you will notice some odd behavior (it’s odd to me anyway). If you uncomment the very first alert, you will see all the correct strings I wish to test, but even though the very last string has the word ‘test’ in it, pattern.test(node.nodeValue) does not yield ‘true’. This is very perplexing.

Naturally I came up with the second alert statement. When the second alert statement is used, the it alerts incorrect strings (and by that I mean it doesn’t alert the very first full one and adds a last one that contains only white space) and the first occurrence of ‘test’ isn’t marked, but the last two are!

I’m fairly new to regular expressions but I thought I understood them well enough until now. Can anyone explain what is happening here?

  • 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-27T21:31:31+00:00Added an answer on May 27, 2026 at 9:31 pm

    Simply remove g from your regex, and the problem is gone.

    foo(node, /(test)/i);
    

    You have a global match flag in your regex. It pushes the pointer forward each time you call test().

    You can read more here:

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test

    As with exec (or in combination with it), test called multiple times
    on the same global regular expression instance will advance past the
    previous match.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.