Question about the find function
-
In the example below, I have a variable that contains a string (Origen), and I want to dynamically extract the value between the first comma and the dot, in other words, the value 300 in this image.
The first TestFind variable attempts to identify the position of “dot,” and note that the return value is zero (0).
So I used the match function to check the term, and it returns the letter “t,” which is exactly at position 0.
To verify that find works with a comma ",", I ran a second test, and it correctly locates the comma at position 4.
Is there a limitation to the
findfunction when searching for text?Thanks.
-
The second argument to
find()is a regular expression, not just a string. Dot.is a special character in regular expressions that matches any character, so your firstfind()returns zero because it quickly matches the first character in the string. The comma doesn't have a special meaning in a regular expression, so it just matches as a comma.If you want to match one of the characters like dot
.that has special meaning in a regular expression, you have to escape it:find(SpeedPTY, "\\.")Now, your approach to solving your problem can be done a little differently/better, I think. Rather than picking apart the string by looking for specific characters, try using
split()to convert the comma-separated string into an array of strings. The first element of the array (at index 0) will be thetrue(as a string, not boolean). The second value (at index 1) will be the string301.5. You can put that intoint(),float(),floor()orround()as needed to make a number out of it, and do whatever you need to do from there.(venv) patrick@drupal:~/Documents/lexpjs$ node cli.js lexp CLI, library version 25308 Type CTRL-C or 'quit' to exit lexpjs> org="true,301.5,150.49,2.09,UFINET PTY,2026-06-27T21:00:20.435581" Result: string true,301.5,150.49,2.09,UFINET PTY,2026-06-27T21:00:20.435581 lexpjs> find(org,".") Result: number 0 lexpjs> find(org,"\\.") Result: number 8 lexpjs> split(org, ",") Result: object [ 'true', '301.5', '150.49', '2.09', 'UFINET PTY', '2026-06-27T21:00:20.435581' ] lexpjs> split(org,",")[0] Result: string true lexpjs> split(org,",")[1] Result: string 301.5 lexpjs>









