简体   繁体   English

基于javascript中的模板来选择字符串部分的最简单方法是什么

[英]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.

相关问题 使用javascript读取/操作查询字符串参数的最简单方法是什么? - What is the easiest way to read/manipulate query string params using javascript? 在javascript中本地化字符串的最简单方法 - Easiest way to localize string in javascript 更改数组值的最简单方法是什么 javascript - What is the easiest way of changing the values of an array javascript 将服务器返回的字符串分配给javascript字符串最简单的方法是什么? - What's the easiest way to assign a string returned from the server to a javascript string? 通过内容,CSS,Javascript或其他方法格式化基于Web的数据的最简单方法是什么? - What's the easiest way to format web-based data by content, CSS, Javascript, or other method? 给定一个表示XML文件的字符串,用Javascript或jQuery查找所有名称空间声明的最简单方法是什么? - Given a string representing an XML file, what's the easiest way to locate all namespace declarations with Javascript or jQuery? 根据另一个下拉列表填充下拉列表的最佳和最简单的方法是什么 - What's the best and easiest way to Populate a dropdown based on another dropdown 根据单词拆分字符串并使用 `^` 符号突出显示部分 - split string based on words and highlighted portions with `^` sign 在Javascript Web脚本和Erlang服务器之间进行通信的最简单方法是什么 - What is the easiest way to communicate between Javascript web script to an Erlang server 将javascript动画置于背景的最简单方法是什么? - What's the easiest way to put the javascript animation to the background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM