Javascript匹配并从字符串中提取$符号? [英] Javascript match and extract $ symbol from a string?

查看:30
本文介绍了Javascript匹配并从字符串中提取$符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有:

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

但是这似乎不起作用.我想提取 $ 符号并进行比较,如果为真,则获取一些货币比较并比较货币.通过正则表达式提取符号的正确方法是什么?

However this doesn't seem to work. I would like to extract the $ symbol and compare it, if true then fetch some currency comparison and compare currency. What is the correct way to extract symbols via regex?

谢谢

推荐答案

这应该有效:'$4000'.match(/^\$/).

(查找字符串开头的$符号)

(It looks for the $ sign at the beginning of the string)

正则表达式字面量的 javascript 语法在表达式的开头和结尾使用 /.

The javascript syntax for regular expression literal uses / at the beginning and at the end of the expression.

这篇关于Javascript匹配并从字符串中提取$符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆