1

I want do all the operation of sql 'like' in javascript. for example:

var str="South-Asia, North-America, South-Africa";

var firstwordmatch=South%; //will result South-Asia,South-Africa
var last match=%ica // will match North-America, South-Africa

What I have tried is the following:

var ex = str.split('%')[1]; // this for matching %ica

var l = str.length;
var n = str.indexOf("%");
// this for matching %ica
if (n == 0) {                   
    var m = c.match(/(.*)ex/)
}
// this for matching 
// this for matching south%
else if (n == l - 1) {
    var m = c.match(/ex(.*)/)
}

but I am unable to write the correct regular expression because it does not get the ex variable.

MAT14
  • 129
  • 4
  • 17

1 Answers1

0

Starts with South: South\S+

In JS:

var myregex = /South\S+/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[0];
} else {
    thematch = "";
}

Ends with ica: \S+ica

In JS:

var myregex = /\S+ica/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
    thematch = matchArray[0];
} else {
    thematch = "";
}

Explanation

  • \S+ matches any chars that are not white-space characters
  • South matches literal chars
  • ica matches literal chars
zx81
  • 41,100
  • 9
  • 89
  • 105