newcomer
newcomer

Reputation: 65

get char before numeric value in jquery

I want to retrieve the char before a numeric value in javascript or jquery.

Eg

$100 should return $

$$100 should return $$

&$ 100 should return &$

$ 25 should return $

possible using regex?

Upvotes: 0

Views: 70

Answers (1)

GoranLegenda
GoranLegenda

Reputation: 591

You can do it like this:

var str = "$$$$34534";
var str2 = "$34534";
var str3 = "$@ 34534";
var str4 = "!@#34534";

var patt = /[\D]*/;
 
 console.log(str.match(patt)[0])
 console.log(str2.match(patt)[0])
 console.log(str3.match(patt)[0])
 console.log(str4.match(patt)[0])

Regex used is simple: /[\D]*/ - match any non-digit char

Upvotes: 2

Related Questions