Having a string in this format
str = "Is toffee=sweet?"
I need to retrieve the first term on the left side of =
, which in this case is toffee
.
To accomplish this, I use the following code snippet:
str.split("=")[0].split(" ").splice(-1,1)[0]
This returns toffee
However, if the string contains extra spaces like below:
str = "Is toffee =sweet?"
The result will be an empty string
Is there a regular expression (regex) that can always capture the first word on the left side of =
, regardless of the number of spaces present?