简体   繁体   English

VueJS:如何删除用户提交字符串的第一个和最后一个引号?

[英]VueJS: How to remove first and last quotation marks of a user submitted string?

I have a VueJS app with a form with pre-populated data.我有一个带有预填充数据表单的 VueJS 应用程序。 A textarea element has some data like so shown on the UI一个 textarea 元素有一些像 UI 上显示的数据

Note: (sorry I do not have enough rep points yet to attach screen shot):注意:(对不起,我还没有足够的代表点来附加屏幕截图):

screenshot of UI text area element with quote marks带引号的 UI 文本区域元素的屏幕截图

I can't modify the way the backend is serving the data.我无法修改后端提供数据的方式。

Thus, is there a good way where I can remove the FIRST and LAST quotation marks from a string?因此,有没有一种好方法可以从字符串中删除 FIRST 和 LAST 引号? I dont' want to touch the inner text in case the user is adding quote marks within.如果用户在其中添加引号,我不想触摸内部文本。

Expected output of 'Hello World, welcome to "fooWorld!"' shall be Hello World, welcome to 'fooWorld!' 'Hello World, Welcome to "fooWorld!"' 的预期输出应为Hello World, Welcome to 'fooWorld!'

Here's a real world example of how I am using the data in my SPA:这是我如何在 SPA 中使用数据的真实示例:

data() {
    return {
        entryNotes: this.post.entryNotes  //<--- this is the data I need to format
    }
}

How about a quick regex to get rid of start and end quotes?一个快速的正则表达式来摆脱开始和结束引号怎么样?

data(){
    return {
        entryNotes: this.post.entryNotes.replace(/^"/,"").replace(/"$/,"")
    }
}

The first regex looks for the start of the string (symbolized by ^) and then a quotation mark.第一个正则表达式查找字符串的开头(用 ^ 表示),然后是引号。 The second regex looks for a quotation mark and then the end of the string (the end of string is symbolized by $).第二个正则表达式查找引号,然后查找字符串的结尾(字符串的结尾由 $ 表示)。

You could also do this:你也可以这样做:

str.match(/^"?(.+?)"?$/)[1]

This one looks for the start of the string (^) followed by a ", but the ? makes it optional. Then the parenthesis captures .+ which means match anything. The ? before the end of the parenthesis means stop matching as soon as you can. Then it says look for a quotation mark, but the next question mark says "possibly", then look for the end of the string ($).这个查找字符串 (^) 的开头,后跟一个 ",但 ? 使它成为可选的。然后括号捕获 .+ 表示匹配任何内容。括号末尾之前的 ? 表示一旦你停止匹配可以。然后它说寻找一个引号,但下一个问号说“可能”,然后寻找字符串的结尾($)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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