admin管理员组文章数量:1334280
I am trying to get the value between dollar sign in a string and put them in a list.
For example, here is a string:
var a ='Dear $name$, This is my number $number$. This is my address $address$ Thank you!'
and what I want to get is,
var b = '$name$,$number$,$address$'
Do I have to take care about the '$' in the string due to $ is also used in jQuery? Do you have any idea about it?
I am trying to get the value between dollar sign in a string and put them in a list.
For example, here is a string:
var a ='Dear $name$, This is my number $number$. This is my address $address$ Thank you!'
and what I want to get is,
var b = '$name$,$number$,$address$'
Do I have to take care about the '$' in the string due to $ is also used in jQuery? Do you have any idea about it?
Share Improve this question edited Jul 17, 2015 at 4:41 Sebastian Simon 19.5k8 gold badges61 silver badges84 bronze badges asked Apr 16, 2015 at 17:18 HaileiHailei 395 bronze badges 2- Can you post what you've tried so far and what's not working in your attempts? – Melissa Avery-Weir Commented Apr 16, 2015 at 17:20
-
1
You want to be careful about
$
because it has special meaning in regular expressions. Nothing in the question, or the answers, has anything to do with jQuery. This is a plain-Javascript question. – Paul Roub Commented Apr 16, 2015 at 17:25
3 Answers
Reset to default 11Use match
:
a.match(/\$.*?\$/g);
This returns an array with all the values. You can also use
a.match(/\$.*?\$/g)||[];
to make sure that you’ve always got an array because if there’s no match you’ll get the null
object which is not always useful.
The RegExp is also explained in an answer of mine to a similar question: match anything (.
), any number of times (*
), as few times as possible (?
).
Then you can use join
to join that Array into a String:
(a.match(/\$.*?\$/g)||[]).join(',');
Code:
var a='Dear $name$, This is my number $number$. This is my address $address$ Thank you!';
var b=(a.match(/\$.*?\$/g)||[]).join(',');
Output:
"$name$,$number$,$address$"
Effectively, in this case the regular expression matches every $
followed by anything up to the next $
and finally that dollar sign at the end. And match
will give a list of results if you specify the g
(global) flag at the end.
As this is a string (and the above a regular expression) literal, there’s no interference with jQuery’s $
symbol. The only important thing is to escape that symbol with a backslash (\$
) because it has a special meaning in RegExp.
get matches with a regular expression:
var matches = a.match(/\$\w+\$/g);
returns an array of matches, so then just join on ma:
var b = matches.join(',');
Essentially you need to split and slice. I made a JSFiddle to show how its done.
Here is the jQuery to acplish what you want.
var a = 'Dear $name$, This is my number $number$. This is my address $address$ Thank you!';
var b = [];
var count = 0;
var split = a.split(" ");
for (var i = 0; i < split.length; i++) {
if (split[i].charAt(0) == "$") {
b[count] = split[i].slice(0, $.inArray("$", split[i], 2)) + "$";
count++;
}
}
本文标签: javascriptHow to get the value between dollar sign in stringStack Overflow
版权声明:本文标题:javascript - How to get the value between dollar sign in string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742370816a2462197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论