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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:25:05+00:00 2026-05-27T09:25:05+00:00

Below you’ll see a javascript/jQuery function I’m currently using: var forward = new RegExp(‘forward’),

  • 0

Below you’ll see a javascript/jQuery function I’m currently using:

            var forward = new RegExp('forward'),
                backward = new RegExp('backward'),              
                left = new RegExp('left'),      
                right = new RegExp('right');                

            if ( tweet.message.match(forward) ) {
                console.log('forward');
                body.addClass('forward');
                bodyRemoveClass();                          
            }

            if ( tweet.message.match(backward) ) {
                console.log('backward');
                body.addClass('backward');
                bodyRemoveClass();                          
            }

            if ( tweet.message.match(left) ) {
                console.log('left');
                body.addClass('left');
                bodyRemoveClass();                          
            }

            if ( tweet.message.match(right) ) {
                console.log('right');
                body.addClass('right');
                bodyRemoveClass();                          
            }                   

Everything works just fine, but I’m not 100% happy with the way it’s written.
Basically what that does is, check if a given keyword (forward, backward, left or right)
are in a tweet (tweet.message)

I would like to know if there is a simple/cleaner way to achieve this.

Sorry but there is no online example…

Thank you

  • 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-27T09:25:05+00:00Added an answer on May 27, 2026 at 9:25 am

    There’s no need to use match() with Regexp here. You can do simple string matching with indexOf(). You can then avoid declaring all the Regexp at the beginning.

            if ( tweet.message.indexOf("forward") > -1) {
                console.log('forward');
                body.addClass('forward');
                bodyRemoveClass();                          
            }
    
            if ( tweet.message.indexOf("backward") > -1) {
                console.log('backward');
                body.addClass('backward');
                bodyRemoveClass();                          
            }
    
            if ( tweet.message.indexOf("left") > -1) {
                console.log('left');
                body.addClass('left');
                bodyRemoveClass();                          
            }
    
            if ( tweet.message.indexOf("right") > -1) {
                console.log('right');
                body.addClass('right');
                bodyRemoveClass();                          
            }               
    

    However, this is much more neatly accomplished with an array of classes:

    // Store your 4 classes in an array
    var classes = ["forward","backward","left","right"];
    for (var i = 0; i<classes.length; i++) {
    
      // Check each class appearing in the message:
      if ( tweet.message.indexOf(classes[i]) > -1) {
         console.log(classes[i]);
         body.addClass(classes[i]);
         bodyRemoveClass();                          
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below I have a Json function and some php code which it retrieves. JavaScript
Below is the jquery code I am using for my AJAX tabs Is there
Below is my Schema. var UserModel = new Schema({ id: ObjectId , email: String
Below is my current char* to hex string function. I wrote it as an
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Below is my validation() function which controls some messages in an alert: function validation()
Below is my code that lets me to move the camera forward it works
Below is a Matlab function that implements the triangulation method as described in Multiple
Below is my class. class MaterialType { public $id; public $name; function getAllMaterialType() {
Below is the code I'm using to run the query, parse the result set,

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.