[英]What is the easiest way to pick portions of a string based on a template in javascript
I'm trying to take values from this string: 我正在尝试从此字符串中获取值:
-webkit-gradient(linear, right top, left top, color-stop(0, #FF7417), color-stop(1, #82A8FF))
and convert it to other formats (-moz,-o-,-webkit-linear etc). 并将其转换为其他格式(-moz,-o-,-webkit-linear等)。 Currently I'm doing this by splitting the string (using '(' , ',' etc) and then re-arranging the pieces into the new format. Is there an easy way to do this ?
目前,我正在通过拆分字符串(使用'(',','等)然后将片段重新排列为新格式来做到这一点,有没有简单的方法呢?
Note: I don't need a way to convert between various gradient formats. 注意:我不需要在各种渐变格式之间进行转换的方法。 I just need a generic way to pick required portions of a string based on a predefined template.
我只需要一种通用方法即可根据预定义的模板来选择字符串的必需部分。
Yo should capture each set with a regular expression, For example /color-stop\\(0,.+?\\)/
should get you the first color-stop part. 您应该使用正则表达式捕获每个集合,例如
/color-stop\\(0,.+?\\)/
应该让您获得第一个颜色停止部分。 Then you just make a new string with each result: 然后,您只需为每个结果创建一个新字符串:
"-webkit-gradient" + color + linear + righttop + etc;
Simple tutorial about regexes and javascript 关于正则表达式和javascript的简单教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.