I have a string like this below:
var st = "ROAM-Synergy-111-222-LLX "
It can have any no. of terms before the numeric values ..ie. its possible formats are:
var st = "SSI-ROAM-Synergy-111-222-LLX " or
var st = "LCD-SSI-ROAM-Synergy-111-222-LLX" etc..
Now I need to fetch only the terms before numeric values in this string. ie. "SSI-ROAM-Synergy" or "LCD-SSI-ROAM-Synergy"
I am using like this:
var finalString = st.split("-");
but how to get only the terms before numeric values.
You can use:
var myval = st.match(/^\D+(?=-)/)[0];
//=> SSI-ROAM-Synergy OR LCD-SSI-ROAM-Synergy
Explanation:
^ assert position at start of the string
\D+ match any character that's not a digit [^0-9]
Quantifier: Between one and unlimited times, as many times as possible
(?=-) Positive Lookahead - Assert that the regex below can be matched
- matches the character - literally
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With