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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:16:44+00:00 2026-05-15T19:16:44+00:00

We have been using the following js/regex to find and replace all non-alphanumeric characters

  • 0

We have been using the following js/regex to find and replace all non-alphanumeric characters apart from – and +

outputString = outputString.replace(/[^\w|^\+|^-]*/g, "");

However it doesn’t work entirely – it doesn’t replace the ^ and | characters. I can’t help but wonder if this is something to do with the ^ and | being used as meta-characters in the regex itself.

I’ve tried switching to use [\W|^+|^-], but that replaces the – and +. I thought that possibly a lookahead assertion may be the answer, but I’m not very sure how to implement them.

Has anyone got an idea how to accomplish this?

  • 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-15T19:16:44+00:00Added an answer on May 15, 2026 at 7:16 pm

    Character classes do not do alternation, hence why the | is literal, and the ^ must be at the start of the class to take effect (otherwise it’s treated literally.)

    Use this:

    [^\w+-]+
    

    (Also, if - is not last, it needs to be escaped as \- inside a character class – so be careful if more characters might be added to the exception list).

    You could also do it with a negative lookahead like this:

    (?![+-])\W
    

    Note: You do not want a * or + after that \W, since the lookahead only applies to the immediately following character (and the g flag makes the replace repeat until done).

    Also note that \w and \W consider _ as a word character. If that’s not desired then to replace that you can use (?![+-])[\W_] (or use explicit ranges in the first expressions).

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

Sidebar

Related Questions

I have been using the following regex to replace all punctuation in a string:
I have been using following Regex to parse @username from posts in my application.
I have the following regex that I have been using successfully: preg_match_all('/(\d+)\n(\w.*)\n(\d{3}\.\d{3}\.\d{2})\n(\d.*)\n(\d.*)/', $text, $matches)
I have been using the following CSS to apply an effect at the bottom
Ok, I have been using the following script for over a year now to
I having trouble in dividing the HTML frames. I have been using the following
I have been following the following post on using multiple ItemTemplates in a ListView
I have been using the following regular expression in ASP.NET and Javascript: [a-zA-ZöäüÖÄÜß0-9]{1}[a-zA-ZöäüÖÄÜß0-9_.\-]{2,14}[a-zA-ZöäüÖÄÜß0-9.!]{1} Now,
I've been using the following site to test a PHP regex so I don't
I have been using the following metod using a for loop but it occurred

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.