Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript strings manipulation

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.

like image 895
vicky Avatar asked Feb 25 '26 11:02

vicky


1 Answers

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
like image 138
anubhava Avatar answered Feb 28 '26 00:02

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!