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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:02:18+00:00 2026-06-02T07:02:18+00:00

I have a search replace script which works to replace strings. It already has

  • 0

I have a search replace script which works to replace strings. It already has options to do case insensitive searches and “escaped” matches (eg allows searching for % ( etc in the search.

How ever I have now been asked to match whole words only, I have tried adding %s to each end, but that does not match words at the end of a string and I can’t then work out how to trap for the white-space items found to leave them intact during the replace.

Do I need to redo the script using string.find and add logic for the word checking or this possible with patterns.

The two functions I use for case insensitive and escaped items are as follows both return the pattern to search for.

    --   Build Pattern from String for case insensitive search
function nocase (s)
      s = string.gsub(s, "%a", function (c)
            return string.format("[%s%s]", string.lower(c),
                                           string.upper(c))
          end)
      return s
    end
function strPlainText(strText)
    -- Prefix every non-alphanumeric character (%W) with a % escape character, where %% is the % escape, and %1 is original character
    return strText:gsub("(%W)","%%%1")
end 

I have a way of doing what I want now, but it’s inelegant. Is there a better way?

   local strToString = ''
     local strSearchFor = strSearchi
    local strReplaceWith = strReplace
    bSkip = false
    if fhGetDataClass(ptr) == 'longtext' then
        strBoxType = 'm'
    end
   if pWhole == 1 then
    strSearchFor = '(%s+)('..strSearchi..')(%s+)'
    strReplaceWith = '%1'..strReplace..'%3'
    end
    local strToString = string.gsub(strFromString,strSearchFor,strReplaceWith)
    if pWhole == 1 then
    -- Special Case search for last word and first word
        local strSearchFor3 = '(%s+)('..strSearchi..')$'
        local strReplaceWith3 = '%1'..strReplace
        strToString = string.gsub(strToString,strSearchFor3,strReplaceWith3)
        local strSearchFor3 = '^('..strSearchi..')(%s+)'
        local strReplaceWith3 = strReplace..'%2'
        strToString = string.gsub(strToString,strSearchFor3,strReplaceWith3)
    end
  • 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-02T07:02:21+00:00Added an answer on June 2, 2026 at 7:02 am

    have a way of doing what I want now, but it’s inelegant. Is there a better way?

    There is an undocumented feature of Lua’s pattern matching library called the Frontier Pattern, which will let you write something like this:

    function replacetext(source, find, replace, wholeword)
      if wholeword then
        find = '%f[%a]'..find..'%f[%A]'
      end
      return (source:gsub(find,replace))
    end
    
    local source  = 'test testing this test of testicular footest testimation test'
    local find    = 'test'
    local replace = 'XXX'
    print(replacetext(source, find, replace, false))  --> XXX XXXing this XXX of XXXicular fooXXX XXXimation XXX    
    print(replacetext(source, find, replace, true ))   --> XXX testing this XXX of testicular footest testimation XXX
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple script that does some search and replace. This is basically
I have a jQuery search script which uses the on keyup function to submit
I have a binary file that I would like to regex search/replace hex bytes
I have to search through a list and replace all occurrences of one element
I have a need to perform search and replace operations on large blocks of
I have a large (600 odd) set of search and replace terms that I
In excel I have to search for specific word in a cell and replace
Im looking for something like a search and replace functionality in Solr. I have
I have search strings, similar to the one bellow: energy food olympics 2010 Terrorism
I have a perl script (or any executable) E which will take a file

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.