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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:39:08+00:00 2026-06-15T18:39:08+00:00

Can you please help me. How can I add this regex (?<=^|\s):d(?=$|\s) in javascript

  • 0

Can you please help me. How can I add this regex (?<=^|\s):d(?=$|\s) in javascript RegExp?

e.g

regex = new RegExp("?????" , 'g');

I want to replace the emoticon :d, but only if it is surrounded by spaces (or at an end of the string).

  • 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-15T18:39:09+00:00Added an answer on June 15, 2026 at 6:39 pm

    Firstly, as Some1.Kill.The.DJ mentioned, I recommend you use the literal syntax to create the regular expression:

    var pattern = /yourPatternHere/g;
    

    It’s shorter, easier to read and you avoid complications with escape sequences.

    The reason why the pattern does not work is that JavaScript does not support lookbehinds ((?<=...). So you have to find a workaround for that. You won’t get around including that character in your pattern:

    var pattern = /(?:^|\s):d(?!\S)/g;
    

    Since there is no use in capturing anything in your pattern anyway (because :d is fixed) you are probably only interested in the position of the match. That means, when you find a match, you will have to check whether the first character is a space character (or is not :). If that is the case you have to increment the position by 1. If you know that your input string can never start with a space, you can simply increment any found position if it is not 0.

    Note that I simplified your lookahead a bit. That is actually the beauty of lookarounds that you do not have to distinguish between end-of-string and a certain character type. Just use the negative lookahead, and assure that there is no non-space character ahead.

    Just for future reference that means you could have simplified your initial pattern to:

    (?<!\S):d(?!\S)
    

    (If you were using a regex engine that supports lookbehinds.)

    EDIT:

    After your comment on the other answer, it’s actually a lot easier to use the workaround. Just write back the captured space-character:

    string = string.replace(/(^|\s):d(?!\S)/g, "$1emoticonCode");
    

    Where $1 refers to what was matched with (^|\s). I.e. if the match was at the beginning of the string $1 will be empty, and if there was a space before :d, then $1 will contian that space character.

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

Sidebar

Related Questions

Can you please help me to add more depth (submenus) to this menu tree?
Can anyone please help me on this: I am trying to add a classname/
Can anyone please help me to add video controls (play, pause, forward, seekbar) to
Can we add image together with text in a picker view? Anyone please help!
Can you please help me with this? I am trying to calculate the total
Please help I search throw whole internet but I can't find answer ... C
Still having trouble can you please help? This needs to be written in Visual
Any body please can please help me, how to center a JFrame on Mac.
Can you please help to derive a regular expression that matches the bold-italics portion
Can you please help me to write a sed command to remove the price

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.