简体   繁体   中英

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). 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. Then you just make a new string with each result:

"-webkit-gradient" + color + linear + righttop + etc;

Simple tutorial about regexes and javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM