HighDots Forums  

Substring in String test, like Python: if "this" in thisString

Javascript JavaScript language (comp.lang.javascript)


Discuss Substring in String test, like Python: if "this" in thisString in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ross
 
Posts: n/a

Default Substring in String test, like Python: if "this" in thisString - 11-03-2009 , 12:11 PM






Does anyone have a suggestion for a nice compact test for a substring
in a string? I'm porting some stuff from Python where I can just test
easily with

a="here are some words"
if "are" in a:
print "Yes it's There"

Something tells me a regex is a likely approach...

Thx in advance...
Ross.

Reply With Quote
  #2  
Old   
Dmitry A. Soshnikov
 
Posts: n/a

Default Re: Substring in String test, like Python: if "this" in thisString - 11-03-2009 , 12:15 PM






On Nov 3, 8:11*pm, Ross <ros... (AT) gmail (DOT) com> wrote:
Quote:
Does anyone have a suggestion for a nice compact test for a substring
in a string? *I'm porting some stuff from Python where I can just test
easily with

* a="here are some words"
* if "are" in a:
* * *print "Yes it's There"

Something tells me a regex is a likely approach...

Thx in advance...
Ross.
'here are some words'.indexOf('are') != -1 && alert('Yes it's There');

Reply With Quote
  #3  
Old   
Stevo
 
Posts: n/a

Default Re: Substring in String test, like Python: if "this" in thisString - 11-03-2009 , 12:24 PM



Ross wrote:
Quote:
Does anyone have a suggestion for a nice compact test for a substring
in a string? I'm porting some stuff from Python where I can just test
easily with

a="here are some words"
if "are" in a:
print "Yes it's There"

Something tells me a regex is a likely approach...

Thx in advance...
Ross.
if ( a.indexOf("are") > -1 )
alert("Yes, it's there");

Reply With Quote
  #4  
Old   
Ross
 
Posts: n/a

Default Re: Substring in String test, like Python: if "this" in thisString - 11-03-2009 , 01:12 PM



Thank you Dmitry and Stevo! That looks great, I can live with that.

Ross.

Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Substring in String test, like Python: if "this" in thisString - 11-04-2009 , 01:29 PM



Ross wrote:

Quote:
Does anyone have a suggestion for a nice compact test for a substring
in a string? I'm porting some stuff from Python where I can just test
easily with

a="here are some words"
if "are" in a:
print "Yes it's There"

Something tells me a regex is a likely approach...
You are on the right track:

if (/are/.test(a))
{
console.log("Yes it's There");
}

The presented solution using String.prototype.indexOf() is probably more
efficient with simple expressions but less flexible. For example, given
text that contains only characters that are also in the ASCII character set,
you can search for a word (here: are):

if (/\bare\b/.test(a))
{
console.log("Yes it's There");
}

And you can search case-insensitive:

if (/\bare\b/i.test(a))
{
console.log("Yes it's There");
}

You cannot do this as easily and as efficiently without Regular Expressions.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.