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

  • Home
  • SEARCH
  • 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 6584393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:55+00:00 2026-05-25T16:31:55+00:00

Well, I thought this would be simple but I am having problems converting some

  • 0

Well, I thought this would be simple but I am having problems converting some PHP regex code to javascript.

Here is my JS code:

for(var i=0; i < badwords.length; i++) {
    var badword = badwords[i].slice(0, -2);
    var regex = "/(";

    for(var j=0; j < badword.length; j++) {
        regex += "[" + badword[j].toLowerCase() + "|" + badword[j].toUpperCase() + "][/W]*";
    }
    regex += ")/";

    msg = msg.replace(new RegExp(regex, "g"), "");
}

I am not getting a match and no replacement is happening. This same code structure and regex format worked in a PHP application.

Any help is appreciated, thanks.

  • 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-25T16:31:56+00:00Added an answer on May 25, 2026 at 4:31 pm

    If your input is foo, your regular expression should be /f[^a-z]*o[^a-z]*o/ig. You can build this regular expression using strings and regular expressions like this:

    var message = 'this string should not contain "foo", "b a r", "b.l.a.h" and "b_a_z". Baaz is acceptable.';
    var badwords = ["foo", "bar", "blah", "baz"];
    for (var i = 0; i < badwords.length; i++) {
        var pat = badwords[i].slice(0, -1).replace(/([a-z])/g, "$1[^a-z]*") + badwords[i].slice(-1);
        var rxp = new RegExp(pat, "ig");
        message = message.replace(rxp, "****");
    }
    message;
    // output:
    // this string should not contain "****", "****", "****" and "****". Baaz is acceptable.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I thought this would be simple and, well, I was wrong. Here is
I know, I would have thought the answer was obviously no as well, but
Well, it may actually be a simple case but I'm having a tough time
It seems I don't understand javascript callbacks quite as well as I thought. In
I thought I understood Java generics pretty well, but then I came across the
Well, this is my first post here and really enjoying the site. I have
I'm trying to write (what I thought would be) a simple bash script that
I have some fundamental points/questions about OpenGL, not all involving code but concepts as
I'm having some problems with the Sql query generated by LINQ, Since my environment
I'm having a very difficult rails problem and i thought to ask for some

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.