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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:01:06+00:00 2026-06-03T08:01:06+00:00

I have a script which takes user input, performs some operations on that input

  • 0

I have a script which takes user input, performs some operations on that input and then executes the modified input.

Example:

User enters ‘vbscipt or javascript’

My script converts it to:

var searchtest = "mystring.search('vbscript')>=0 || mystring.search('javascript')>=0";

And executes it using eval(searchtest);

However if the user enters ‘vbscript javascript’

My script converts it to

var searchtest = "mystring.search('vbscript')>=0 mystring.search('javascript')>=0";

(Note lack of ||) Which will cause an error when I call eval(searchtest);

Is there a way to test the searchtest string to determine if it is a valid javascript expression before executing 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-06-03T08:01:07+00:00Added an answer on June 3, 2026 at 8:01 am

    JavaScript eval is evil !!

    Instead of transforming user input as you are doing why not something like the following :

    // list of values considered valid
    myString = ['javascript', 'vbscript', 'perl', 'python'];
    
    function search (x) {
      // Returns true if x is a value an array (passed as `this`)
      return this.indexOf (x) >= 0;
    }
    
    function searchTest (userInput, myString, search) { 'use strict';
    
      function orClause (clause) {
        // caluse is a series of values separated by `or`, return true
        // if any search returns true for any of the values.
        clause = clause.split (/\s+or\s+/i); // split on 'or'
        while (clause.length) { // for each value
          if (search.apply (myString, [clause.shift ()])) {
            return true;
          }
        }
        return false; // no matches....
      }        
    
      userInput = userInput.split (/\s+and\s+/i); // split on 'and'
    
      // userInput is now an array of clauses each is either a value or 
      // a series of values separated by `or`. We pass each clause to
      // the orCaluse function and return true only if ALL returns are true. 
      while (userInput.length) { 
        if (!orClause (userInput.shift ())) { 
           return false; // and fails
        }
      }
    
      return true;  // All true so return true.
    }
    
    searchTest ('fred or javascript and python', myString, search),
    

    I am assuming your expresson can be a sequence of ands and ors with ands taking precedence

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

Sidebar

Related Questions

I have to use a provided script that takes user input while the script
I have a script which takes user input, the REFRESH option is optional. I
I have a simple search script that takes user input and searches across directories
We have a script that parses XML feeds from user generated sources which from
I have a Perl script which nests foreach loops as seen below. It takes
I have a perl script, which takes a query string parameter, connects to a
i have a script which take hh:mm:ss inputs for example vid_cut.py -t 00:00:30 00:10:00
I have a script which creates a user-defined object like this: obj = new
I have script which allows the user to choose an image and write something
I have a bash script that takes the date, month and year as separate

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.