[英]Add a character to the beginning and end of a string
I am using a platform which uses a JavaScript-like syntax and allows to convert my values using regex.我正在使用一个平台,该平台使用类似 JavaScript 的语法并允许使用正则表达式转换我的值。
I need to add a single quote at the beginning and at the end of a string, the string will always have 9 characters.我需要在字符串的开头和结尾添加一个单引号,该字符串将始终有 9 个字符。
Ie: IE:
123456789
should be converted to '123456789'
123456789
应转换为'123456789'
What expression should I use?我应该用什么表达方式?
You don't need a regular expression.你不需要正则表达式。
str = "123456789";
str = "'" + str + "'";
Although this question was asked a little early, newly defined backticks
standard called Template Literals from ES6 provides new possibility to solve this question with a simpler syntax:尽管提早提出了这个问题,但新定义的backticks
标准称为 ES6 中的模板文字,提供了使用更简单语法解决此问题的新可能性:
const str = '123456789';
const wrapped = `'${str}'`;
If its like Javascript you can do something like...如果它像 Javascript 你可以做类似的事情......
var myString = "'" + oldString + "'";
Where oldString is your 123456789其中 oldString 是你的 123456789
str = "123456789"; str = str.replace(/^/,"'").replace(/$/,"'") console.log(str)
You can use literal \
\'123456789\'.您可以使用文字\
\'123456789\'。
Why not just coerce to a String type:为什么不强制转换为 String 类型:
var strValue = String(123456789);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.