1

I am working on matching the name for eg. name in the response says:

'World map'

Now I want to write a generic regex in the karate jsonpath which should work for the word "map" irrespective of its position in the multiple words for eg - One common script should match below permutations.

1 - Climate map 2 - Wolds map 3 - Worlds China map 4 - Big map is too small 5 - Big map is there

What I have written so far, which seems to be not working

$..source..[?(@.name =~ /(\bmap\b)/i)]

Can anyone please help what exact regex should I put which would check 'map' world in the example above.

vdrulerz
  • 264
  • 3
  • 13

1 Answers1

1

Sometimes it is simpler to use pure Java:

* def str = 'foo map bar'
* assert str.contains('map')

* def hasMap = function(x){ return x.contains('map') }
* assert hasMap(str)

So don't use a regex, please refer this part of the docs: https://github.com/intuit/karate#self-validation-expressions

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks for your message, but how would I be avoiding the capitalization with your java logic (ignoreUpperCase something like that). – vdrulerz Sep 20 '19 at 05:42
  • @vdrulerz `x.toLowerCase().contains('map')` – Peter Thomas Sep 20 '19 at 05:45
  • @ptrthomas Your solution would only work if you have just one node but when I will parse the response for all nodes in the array it would always pass because of the str.contains Is there any way that I do this assertion on the arrays for each array object ? for eg - * def getName = get response.source[*].name now if I print getName it will show [ "world map", "state map", "town map area" ] Also can we do contains and starts with assertion in one line of code ? – vdrulerz Sep 20 '19 at 05:52
  • @vdrulerz please please please read the docs. look for `match each` and then ask a SEPARATE question if needed. thanks – Peter Thomas Sep 20 '19 at 05:53