Solved

How to find a string in a variable field using javascript

Posted on 2016-07-22
8
68 Views
Last Modified: 2016-07-23
Hello,   I am new to javascript and been searching for a way to search for a string to be found in a variable field.  
Example:  
Have a variable x    x="Hello World"   I would like to check the variable x to see if it contains "He".  
I thought I could use something like this:
if (trimTrailingSpaces(x)=="He*") {     } .
.
Is there a  way to do this using javascripts.
Like I said I am new to this.  
Any help would be appreciated.
Thanks
0
Comment
Question by:tambrosi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Assisted Solution

by:Eric C
Eric C earned 125 total points
ID: 41725213
EXAMPLE:

var myList = "Ford, Chevy, and Toyota";
var mySearch = "Audi";

// function
function findInList (vehicle) {
    if (myList.indexOf(vehicle) >= 0) {
        console.log(vehicle + " is in the string");
    }
    else {
        console.log(vehicle + " is not in the string.");
    }
}


// call the function
findInList(mySearch);
0
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 125 total points
ID: 41725332
if(x.indexOf("He") > -1){
  //success
}
0
 
LVL 5

Expert Comment

by:Eric C
ID: 41725348
My comment is the same result ... > -1 is the same as >= 0.     ;-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:gurpsbassi
ID: 41725357
My comment is the same result ... > -1 is the same as >= 0.     ;-)

yes depends on coding style.
both are valid.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41725419
Posting JavaScript questions in the Java TA is not appropriate, and might make your question less likely to be answered. http://technojeeves.com/joomla/index.php/free/127-javascript-is-not-java
2
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41725576
if (trimTrailingSpaces(x)=="He*") {     }
If you were using a search method that recognizes the asterisk as a wild card character, the above condition would be looking for a string that begins with "He", not just contains "He". Which do you wish to search for, contains or begins with?
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 41725585
Given your "He*" example, I am going to assume you like to think in pattern matching style.

In Javascript one would use a regular expression to achieve this:

if (x.match(/He/)) { .... }

Open in new window


If you want to make sure the variable starts with "He", you would need to add a begin of line marker:
if(x.match(/^He/)) { ... }

Open in new window


You can play with regular expressions at various sites. I just looked up this one on a search engine: regexr.com

Kind regards,

-r-
0
 

Author Closing Comment

by:tambrosi
ID: 41725759
First, sorry I posted question to wrong area.
Thanks for all of the answers, all of them posted really helped me understand the javascripting code stream.  That was the first time I looked/tested the function process, and it will be something I can use in the future.
Like I said, I am really new to this and really appreciated the help!!
Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question