Search code examples
regexkaratejsonpathjson-path-expression

Karate framework text match startsWith as well as contains with OR clause


I am parsing a response for all the names object and it returns an array

* def getName = response.source[*].name

And print getName

The api response looks like

[print] [
"Map of USA",
"Global map",
"Check map of RSA"
]

I want to match whether the getName[*] starts with 'map' or contains 'map' by ignoring the upper case also. I am able to do the contains match using -

* def getLowerCase = karate.lowerCase(getName)
* match each getLowerCase[*] contains 'map'

But not sure how to put the startsWith condition also in the same line. Can anyone help me out with the logic. Thanks in advance !


Solution

  • Here you go:

    * def names = ['map foo', 'foo map', 'f map oo']
    
    * def hasMap = function(x){ return x.startsWith('map') || x.toLowerCase().contains('map') }
    * match each names == '#? hasMap(_)'