Searching character's

#1

i want to check if have some words in a phrase, like "Bom". Think that is how to start but dont know how to check if "Bom" exist in my phrase.Captura%20de%20tela%20de%202018-08-16%2015-02-03

0 Likes

#2

Try the switch node "Contains" option.

0 Likes

#3

I need to check if my msg.payload out have the work "bom". I put the switch node and dont have success

0 Likes

#4

This should help you

[{"id":"c4dc9486.8bb738","type":"inject","z":"dc97015.997e9","name":"","topic":"","payload":"this is a string with bom in it","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":610,"y":80,"wires":[["19c6b999.e75d56"]]},{"id":"19c6b999.e75d56","type":"switch","z":"dc97015.997e9","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"bom","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":770,"y":80,"wires":[["8b5d4786.14d8a8"]]},{"id":"8b5d4786.14d8a8","type":"debug","z":"dc97015.997e9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1210,"y":80,"wires":[]}]

0 Likes

#5

The contains option is case sensitive. You need to use every combination you can figure out if you need to find them from arbitrary text.
Bom, BOM, and is the "!BOM!" as dangerous as the "bOm" :slight_smile:

0 Likes

#6

or copy msg.payload to msg.lowercase and force the contents of that to lowercase and then run the search on that

0 Likes

#7

And then it's time for regex :laughing:

1 Like

#8

I used the code below to set wath i was looking for. Thanks U guys.
var msgList = [];

var letterList = msg.payload.output.text[0];

letterList = letterList.toString('uft8');

var tam_msg = letterList.length;

for (var i = 0; i < tam_msg; i++){

if(letterList[i] == 'b' && letterList[i+1] == 'o' && letterList[i+2] == 'm'){

//code_below

}

}

return [msgList];

0 Likes

#9

It's probably worth looking at the JavaScript .indexOf function.

0 Likes

#10

JavaScript has lots of ways to check if a string appears within another string without you having to loop over the individual characters.

if (/bom/.test(msg.payload.output.text[0])) {

}
if (msg.payload.output.text[0].indexOf("bom") > -1) {

}
0 Likes

#11

Seems a lot easier to use a switch node with a regex:

4 Likes

#12

Try to avoid using the function for things that can be solved with the "Switch" or the "Change", soon you will realize how powerful are those 2 nodes.
#fikdica

0 Likes