Reputation: 2475
I am using jQuery DataTable to display records and I want to search using RegExp in certain scenario
I am having string as:
var str="ATP ,Defib 30 J (830 V),Defib 30 J (830 V),Aborted Defib 30 J (830 V),Aborted CVRT 15 J (588 V)";
if I search using only CVRT, then It should not be matched, but if I searched using Aborted CVRT, it should be matched. i.e. If I enter text starting after comma,text should be matched like searching with Aborted CVRT or Defib 30, Also should match ATP (first in string).
Thanks in Advance
Upvotes: 0
Views: 110
Reputation: 6761
If I understand correctly, you should use two RegExs, one for the Defib 30
, other for ATP
.
Something like these two:
,\w+ \w+
Matches: ,Defib 30
, etc.
^\w+
Matches: ATP
.
If you don't need that ,
, you can replicate positive look behind:
var matches = [], str = "ATP ,Defib 30 J (830 V),Defib 30 J (830 V),Aborted Defib 30 J (830 V),Aborted CVRT 15 J (588 V)";
str.replace(/(,)(\w+ \w+)?/g, function(match, $0, $1){
if($0) matches.push($1)
});
console.log(matches); // ["Defib 30", "Defib 30", "Aborted Defib", "Aborted CVRT"]
Upvotes: 1
Reputation: 15393
Use string .match() in javascript.The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
var str = "ATP ,Defib 30 J (830 V),Defib 30 J (830 V),Aborted Defib 30 J (830 V),Aborted CVRT 15 J (588 V)";
var res = str.match(/CVRT/g);
console.log(res);
Upvotes: 0