Javascript match and extract $ symbol from a string?

后端 未结 4 848
孤街浪徒
孤街浪徒 2021-01-24 02:55

Currently I have:

var price = \'$4000\';
var currency = price.match([\\$]);
alert(currency);

However this doesn\'t seem to work. I would like t

4条回答
  •  盖世英雄少女心
    2021-01-24 03:29

    If you want to find the dollar sign in any position use:

    var currency = price.match(/\$/);
    

    If you want to find the dollar sign at the beginning of the string use:

    var currency = price.match(/^\$/);
    

    Here's the documentation about Javascript RegExp

提交回复
热议问题