/    Sign up×
Community /Pin to ProfileBookmark

"Contains" in JS

Hi,

I am new here. Can you help me please…

if I have a text for example:

“The old man is fishing all day”

How can i ask js if the text contains the word man?

greetings me

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@lafmafinauthorMay 22.2016 — http://www.w3schools.com/jsref/jsref_indexOf.asp[/QUOTE]

Thank you, what are the numbers? I need something like:

var x = Earth is a planet

if (x contains planet)

{

react();

}
Copy linkTweet thisAlerts:
@daveyerwinMay 22.2016 — var str = 'Earth is a planet';

alert(str.indexOf('planet'))//alerts 11
Copy linkTweet thisAlerts:
@rootMay 22.2016 — and you can use a regular expression

<i>
</i>s = "man";
console.log( "--------------------------------------------" );
console.log( s + " with no express conditions or rules");
str1 = "The old man is fishing all day";
str2 = "The old woman is gutting fish all day";
var reg = /man/;
s1 = "&gt; "+ s +" is "+ (reg.test(str1)?"":"not ") +"in "+str1;
s2 = "&gt; "+ s +" is "+ (reg.test(str2)?"":"not ") +"in "+str2;
console.log(s1);
console.log(s2);
console.log( "--------------------------------------------" );
console.log( s + " with word boundaries");
var reg = /bmanb/;
s1 = "&gt; "+ s +" is "+ (reg.test(str1)?"":"not ") +"in "+str1;
s2 = "&gt; "+ s +" is "+ (reg.test(str2)?"":"not ") +"in "+str2;
console.log(s1);
console.log(s2);
console.log( "--------------------------------------------" );
s = "Man";
console.log( s + " as case insensitive without word boundaries");
var reg = /Man/i; // case insensitive
s1 = "&gt; "+ s +" is "+ (reg.test(str1)?"":"not ") +"in "+str1;
s2 = "&gt; "+ s +" is "+ (reg.test(str2)?"":"not ") +"in "+str2;
console.log(s1);
console.log(s2);
console.log( "--------------------------------------------" );
console.log( s + " as case insensitive with word boundaries");
var reg = /bManb/i; // case insensitive with word boundaries
s1 = "&gt; "+ s +" is "+ (reg.test(str1)?"":"not ") +"in "+str1;
s2 = "&gt; "+ s +" is "+ (reg.test(str2)?"":"not ") +"in "+str2;
console.log(s1);
console.log(s2);
console.log( "--------------------------------------------" );


outputs the following --------------------------------------------
man with no express conditions or rules
<i>&gt; </i>man is in The old man is fishing all day
<i>&gt; </i>man is in The old woman is gutting fish all day
-------------------------------------------
man with word boundaries
<i>&gt; </i>man is in The old man is fishing all day
<i>&gt; </i>man is not in The old woman is gutting fish all day
--------------------------------------------
Man as case insensitive without word boundaries
<i>&gt; </i>Man is in The old man is fishing all day
<i>&gt; </i>Man is in The old woman is gutting fish all day
--------------------------------------------
Man as case insensitive with word boundaries
<i>&gt; </i>Man is in The old man is fishing all day
<i>&gt; </i>Man is not in The old woman is gutting fish all day
--------------------------------------------


F12 opens the console.log where the output goes to...

Regular Expressions are allot more powerful but can cause headaches.
Copy linkTweet thisAlerts:
@Ay__351_eMay 27.2016 —  <br/>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;script&gt;
String.prototype.contains = function(kelime){
for(var i = 0; i &lt; this.length; i++){
// alert(this.slice(i,kelime.length + i ) );
if(this.slice(i,kelime.length + i ) == kelime ){
return true;
}
}
return false;
}


var x = "Earth is a planet";
if (x.contains("planet") )
{
alert("Aranan kelime cümlenin içinde var. ");
//react();
}

&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

http://www.w3schools.com/jsref/jsref_slice_string.asp
×

Success!

Help @lafmafin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...