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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:15:30+00:00 2026-06-08T01:15:30+00:00

I have following string sssHi this is the test for regular Expression,sr,Hi this is

  • 0

I have following string

sssHi this is the test for regular Expression,sr,Hi this is the test for regular Expression

I would like to replace only

Hi this is the test for regular Expression

segment with some other string.

the first segment in string “sss Hi this is the test for regular Expression” should not be replaced

I wrote following regex for the same :

/([^.]Hi\sthis\sis\sthe\stest\sfor\sregular\sExpression)|(Hi\sthis\sis\sthe\stest\sfor\sregular\sExpression)$/

but it matches both segments. I want to match only the second one as first segement is prefixed by “sss”.

[^.]      

should match nothing except newline right? So group

  "([^.]anystring)"

should only match “anystring” that is not preceded by any chanrachter except newline.
Am I correct?

any thoughts.

  • 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-08T01:15:31+00:00Added an answer on June 8, 2026 at 1:15 am

    Matching a string that is not preceded by another string is a negative lookbehind and not supported by JavaScript’s regex engine. You can however do it using a callback.

    Given

    str = "sssHi this is the test for regular Expression,sr,Hi this is the test for regular Expression"
    

    Use a callback to inspect the character preceding str:

    str.replace(/(.)Hi this is the test for regular Expression$/g, function($0,$1){ return $1 == "s" ? $0 : $1 + "replacement"; })
    // => "sssHi this is the test for regular Expression,sr,replacement"
    

    The regex matches both strings so the callback function is invoked twice:

    1. With
      • $0 = "sHi this is the test for regular Expression"
      • $1 = "s"
    2. With
      • $0 = ",Hi this is the test for regular Expression"
      • $1 = ","

    If $1 == "s" the match is replaced by $0, so it remains unchanged, otherwise it is replaced by $1 + "replacement".

    Another approach is to match the second string, i.e. the one you want to replace including the separator.

    To match str preceded by a comma:

    str.replace(/,Hi this is the test for regular Expression/g, ",replacement")
    // => "sssHi this is the test for regular Expression,sr,replacement"
    

    To match str preceded by any non-word character:

    str.replace(/(\W)Hi this is the test for regular Expression/g, "$1replacement")
    // => "sssHi this is the test for regular Expression,sr,replacement"
    

    To match str at the end of line:

    str.replace(/Hi this is the test for regular Expression$/g, "replacement")
    // => "sssHi this is the test for regular Expression,sr,replacement"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following string String str = replace :) :) with some other string;
I have the following string: \\\\AAA.AA.A.AA\\d$\\ivr\\vm\\2012May\\29\\10231_1723221348.vox I would like it to look like: \\AAA.AA.A.AA\d$\ivr\vm\2012May\29\10231_1723221348.vox
I have following string hello[code:1], world [code:2], hello world I want to replace this
I have the following string : str = {application.root.category.id:2} I would like to convert
So I have following string: var s = '<span>Some Text</span> Some other Text'; The
Suppose I have following string: String asd = this is test ass this is
I have the following string: cn=abcd,cn=groups,dc=domain,dc=com Can a regular expression be used here to
I have the following string and I would like to remove <bpt *>*</bpt> and
I have the following string, which matches the following RegEx string. I would like
I have the following: string test = 9586-202-10072 How would I get all characters

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.