如何获得美元符号之间的价值?

时间:2021-12-29 21:47:43

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?

我是否必须关注字符串中的'$',因为$也用于jQuery?你有什么想法吗?

3 个解决方案

#1


11  

Use 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 (?).

RegExp也在我的回答中解释了类似的问题:匹配任何(。),任意次数(*),尽可能少的次数(?)。

Then you can use join to join that Array into a String:

然后,您可以使用join将该Array加入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.

实际上,在这种情况下,正则表达式匹配每个$,后跟任何直到下一个$,最后是美元符号。如果在结尾指定g(全局)标志,匹配将给出结果列表。

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.

由于这是一个字符串(以及上面的正则表达式)文字,因此不会干扰jQuery的$符号。唯一重要的是用反斜杠(\ $)来逃避该符号,因为它在RegExp中具有特殊含义。

#2


0  

get matches with a regular expression:

与正则表达式匹配:

var matches = a.match(/\$\w+\$/g);

returns an array of matches, so then just join on comma:

返回一个匹配数组,所以只需加入逗号:

var b = matches.join(',');

#3


0  

Essentially you need to split and slice. I made a JSFiddle to show how its done.

基本上你需要分割和切片。我做了一个JSFiddle来展示它是如何完成的。

Here is the jQuery to accomplish what you want.

这是jQuery来完成你想要的。

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++;
    }
}

#1


11  

Use 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 (?).

RegExp也在我的回答中解释了类似的问题:匹配任何(。),任意次数(*),尽可能少的次数(?)。

Then you can use join to join that Array into a String:

然后,您可以使用join将该Array加入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.

实际上,在这种情况下,正则表达式匹配每个$,后跟任何直到下一个$,最后是美元符号。如果在结尾指定g(全局)标志,匹配将给出结果列表。

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.

由于这是一个字符串(以及上面的正则表达式)文字,因此不会干扰jQuery的$符号。唯一重要的是用反斜杠(\ $)来逃避该符号,因为它在RegExp中具有特殊含义。

#2


0  

get matches with a regular expression:

与正则表达式匹配:

var matches = a.match(/\$\w+\$/g);

returns an array of matches, so then just join on comma:

返回一个匹配数组,所以只需加入逗号:

var b = matches.join(',');

#3


0  

Essentially you need to split and slice. I made a JSFiddle to show how its done.

基本上你需要分割和切片。我做了一个JSFiddle来展示它是如何完成的。

Here is the jQuery to accomplish what you want.

这是jQuery来完成你想要的。

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++;
    }
}