2

I have got of array symbols as shown below

var sourcesymbols = ["ERT", "UBL" , "AMAZING"]; 

I am getting the following news title from rss feed

you experts are amazing

How to check if the content present in the rssfeedstring is present under the sourcesymbols array or not ??

For example rssfeedstring has word amazing and it is also present under sourcesymbols

please let me know how to achive this .

I have tried to convert the rssfeedstring to uppercase then i am not sure how to use the indexOf on the string .

rssfeedstring = rssfeedstring.toUpperCase();

please let em know if there is any better approach also for doing this as the array will have 2000 symbols

http://jsfiddle.net/955pfz01/3/

Tushar
87.3k21 gold badges164 silver badges182 bronze badges
asked Oct 16, 2015 at 13:26

2 Answers 2

2

You can use regex.

Steps:

  1. Convert the array to string with join using |(OR in regex) as glue
  2. Use \b-word boundary to match exact words
  3. Use i flag on regex to match irrespective of the case. So, don't have to change the case of string.
  4. Escape the slashes as using RegExp constructor requires string to be passed and \ in string is used as escape following character.
  5. test can be used on regex to check if the string passes the regex.

var sourcesymbols = ["ERT", "UBL", "AMAZING"];
var mystr = 'you experts are amazing';
var regex = new RegExp("\\b(" + sourcesymbols.join('|') + ")\\b", 'i'); // /\b(ERT|UBL|AMAZING)\b/i
alert(regex.test(mystr));


You can also use some

  1. Convert the string to array by using split with \s+. This will split the string by any(spaces, tabs, etc) one or more space character
  2. Use some on splitted array
  3. Convert the string to uppercase for comparing
  4. Check if the element is present in array using indexOf

var mystr = 'you experts are amazing';
var sourcesymbols = ["ERT", "UBL", "AMAZING"];
var present = mystr.toUpperCase().split(/\s+/).some(function(e) {
 return sourcesymbols.indexOf(e) > -1;
});
alert(present);

answered Oct 16, 2015 at 13:28

5 Comments

Could you please let me provide a example .
FYI to OP this will match BERT.
@epascarello Thanks for pointing out. Used \b word boundary
@Tushar , could you please look at this fiddle jsfiddle.net/tp11h9yv/4 , why am i getting true in this case ??
@PreethiJain Please check jsfiddle.net/nmgxw2c4, missed (), updating answer
0

Try using Array.prototype.map() , Array.prototype.indexOf() to return matched text, index of matched text within sourcesymbols

var sourcesymbols = ["ERT", "UBL" , "AMAZING"]; 
var mystr = 'you experts are amazing';
var res = mystr.split(" ").map(function(val, index) {
 var str = val.toUpperCase(), i = sourcesymbols.indexOf(str);
 return i !== -1 ? [val, i] : null
}).filter(Boolean);
console.log(res)

jsfiddle http://jsfiddle.net/955pfz01/6/

1 Comment

Updated your code jsfiddle.net/8nsz1goj. Feel free to update answer, if this improves code

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.