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

The Archive Base Latest Questions

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

I’m trying to match usernames within a string like: user: hi, has anyone seen

  • 0

I’m trying to match usernames within a string like:

"user: hi, has anyone seen user today user"

The cases to match:

  • substring is the first word trailing a space, in the middle surrounded by spaces or the last and leading a space
  • Following characters are allowed to trail the word but not returned as a result: “:;,”

The following matches all the cases but returns unwanted spaces and characters (I only want to replace usernames):

/(^(user)[\s|:|;|,])|(\s(user)[\s|:|;|,]?\s)|(\s(user))/gi

In the end I want to replace only username with links.

EDIT: Note that the username can’t be matched if it’s part of url or other string, except cases when special characters are trailing it.

  • 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-27T01:30:04+00:00Added an answer on May 27, 2026 at 1:30 am

    Depending upon how transparent you want it to be to users (or what your eventual goal is), you may consider requiring someone to put a symbol (such as @) before a user name, so that they can elect whether or not to have a link to the user…

    Aside from that, your expression has several potential errors: character classes (denoted by []) treat nearly all characters literally, including |, the entire alternation syntax makes the third alternation ((\s(user))) into something that will allow matches to userSmith or userJones and not just user – which is something I think you specifically want to disallow…

    I think you are asking for something like this:

    (^|\s)(user)(?=[:;,\s]|$)
    

    this breaks down to:

    (^|\s)      # either assert that this is the beginning, or capture a whitespace character; capture into back-reference #1
    (user)      # capture the username 'user' exactly 
    (?=         # look-ahead to verify that the following CAN be matched
       [:;,\s]  #    one character that is      :  ;  ,  <or whitespace>
       |        #     -OR-
       $        #    the end of the string
    )           # end look-ahead
    

    However, there are a few cases that you might want to consider. By not allowing several types of punctuation after the username, you will exclude results from strings like: Let me know if you see user., have you seen user? or I really like user! – the rejection for URLs should already be accomplished by requiring whitespace (or the beginning of the string) before user – not allowing such punctuation afterwards will reject some cases I think you will want to match. You could simply add in this extra punctuation:

    (^|\s)(user\b)(?=[;:,.?!)"\s]|$)
    

    But I would suggest something more like the following (removing the following-punctuation requirement):

    (^|\s)(user\b)
    

    I’ve put all three suggestions on jsFiddle, to show you what you get and allow you to put some of your own strings in.

    Which ever way you prefer, these expressions would be used in a find-replace wherein you would replace the whitespace consumed before the user’s name with itself in the replace expression:

    source.replace(/(^|\s)(user\b)/gi, '$1<a href="/linkToProfile?n=$2">$2</a>')
    

    Though I’m pretty sure I answered the question, please let me know if there are cases you specified that aren’t covered!

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.