Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

Can any one please provide a JavaScript function or Regexp for the below mentioned scenario.

For Example: my string is :
1) 'My String abc-s'.

I have array of items to match the word in the above string.
var items= ["abc", "def", "ghi"];

If the above string contains any of the array item then it has to return true.
In the above array of items not contain 'abc', so it has to return false.


2)
var items= ["abc-s", "def-s", "ghi-s"];

If the above string contains any of the above item then it has to return true.


What I have tried:

Hi Everyone,

Can any one please provide a JavaScript function or Regexp for the below mentioned scenario.

For Example: my string is : 
1)  'My String abc-s'.

I have array of items to match the word in the above string.
<pre>var items= ["abc", "def", "ghi"];

If the above string contains any of the array item then it has to return true.
In the above array of items not contain 'abc', so it has to return false.


2)
var items= ["abc-s", "def-s", "ghi-s"];

If the above string contains any of the above item then it has to return true.
Posted
Updated 10-Jan-17 5:48am
Comments
Nathan Minier 10-Jan-17 7:11am    
Okay, when you're developing a REGEX you need to determine the unique characteristics of the substring that you're trying to isolate. In this case, is it always at the end of a line? Is it always just those 3 capture patterns? Could you do a capture group and run it through a switch? These are important considerations.

\b - indicates beginning or end of word, so you could create expression like below
(\babc\b|\bdef\b|\bhgi\b)

you could try it online at Scriptular - Javascript Regular Expression Editor[^]
 
Share this answer
 
Comments
suman palla 10-Jan-17 8:32am    
Thank you Anup. Please see below.

I Have to two types of array times.
1) var items= ["abc", "def", "ghi"];
2) var items= ["abc-s", "def-s", "ghi-s"];

For the First array items the String is 'My String abc-s'
var items= ["abc", "def", "ghi"];
var myString ='My String abc-s'
The string contains abc-s, so it not a valid.
If user gives 'My String abc' then only it should valid.


Second Scenario:
var items= ["abc-s", "def-s", "ghi-s"];
var myString ='My String abc'
The string contains abc, so it not a valid.
If user gives 'My String abc-s' then only it should valid.
anup.bhunia 10-Jan-17 12:30pm    
your first scenario should be covered with the solution, i provided.

But second one, it would be tricky as your words "abc-s" itself contains a word separator "-". You may frame your problem differently.
Check this out:
//var str = "My String abc";
var str = "My String abc-s";

var items= ["abc", "def", "ghi"];
//var items = ["abc-s", "def-s", "ghi-s"];

for (i=0; i < items.length; i++){
  var pattern = new RegExp("(<=\\s|\\b)"+ items[i] +"(?=[]\\b|\\s|$)");
	alert(pattern.test(str));
}
See demo at: JSFiddle[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900