0

Hy, I have a long csv and I want to search strings that contain certain words, for example:

var str = "Dan Jobsy,Google Analitis,ejobs";

I found this:

(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)

that will parse the CSV and return the words, what I want to know how to extend this and add a condition to return only words in csv that contain a certain string? Like if I search "oo" it will return only "Google Analitics", but if I search "job" it will return "Dan Jobsy" and "ejobs"

EDIT:

Didn't find a solution here, except the one using the regexp I supplied, if I can manage to solve this on my own I will post the ans here.

cuzzea
  • 1,515
  • 11
  • 22
  • 1
    See [my answer](http://stackoverflow.com/a/8497474/433790) to a very similar question: [How can I parse a CSV string with Javascript?](http://stackoverflow.com/q/8493195/433790) – ridgerunner Dec 17 '11 at 19:45
  • nice but what I want is a way to search and return only the values that contain a keyword in the csv items, 10+ for explication :) – cuzzea Dec 18 '11 at 00:20

3 Answers3

0

String.search(pattern) :

Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not.

But to get the Regular Expression you need you have to study a bit regular expression patterns. you could start here or google search javascript regexp tutorial

Spyros Mandekis
  • 984
  • 1
  • 14
  • 32
0
var str     = "Dan Jobsy,Google Analitis,ejobs";

var needle  = "job";
var matches = [];

for (i in A = str.match (/(\"(?:[^\"]+|\"\")*\"|[^,]+)(?=$|,)/g)) {
  if (A[i].toLowerCase().indexOf (needle.toLowerCase ()) != -1)
    matches.push (A[i]);
}

console.log (matches);

output

[ 'Dan Jobsy', 'ejobs' ]
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • this is nice but it's not done via the regexp, I want to skip the extra search in the for – cuzzea Dec 17 '11 at 16:27
  • If you'd like to input your needle directly into the regular expression the code will get **VERY** messy, and you'll need to escape your needle as well (otherwise you are not going to search for a string such as `"StackOverflow?"`. I see no reason why'd you wanna complicate things, especially since you asked for a solution to a problem where you don't even care learning enough to solve it yourself. – Filip Roséen - refp Dec 17 '11 at 16:34
  • I care learning about, but I don't think I would find the solution in 1-2 days, I understand how the above regexp works and managed to search for a string (it will return the searched strings starting with my search string). the thing is that I have a very big csv that needs fast searching, and regexp is the best way to do it – cuzzea Dec 17 '11 at 16:54
  • you obviously have a lot to learn when you write **needs fast searching** and **regexp is the best way** in the same sentence.. – Filip Roséen - refp Dec 17 '11 at 16:55
  • you mean I need to learn regexp or that there is a better solution for this than regexp? – cuzzea Dec 17 '11 at 16:57
  • If you are looking for performance you should get as far away from regular expressions as possible. – Filip Roséen - refp Dec 17 '11 at 17:01
0

To split a javascript string by a delimiter, use string.split(str or regex);

So:

var str = "Dan Jobsy,Google Analitis,ejobs";
var ary_names = str.split(','), i = 0, ary_len = array.length, count = 0;
for (i = 0; i < ary_len; i++){
  if (ary_names[i].match(/jobs/i)){ count++; }
}
// count is the # of times jobs was found.

from there you can use a loop, indexOf, include with a framework, etc on the array to search through.

CodeJoust
  • 3,760
  • 21
  • 23