简体   繁体   中英

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. A textarea element has some data like so shown on the UI

Note: (sorry I do not have enough rep points yet to attach screen shot):

screenshot of UI text area element with quote marks

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? 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!'

Here's a real world example of how I am using the data in my 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 ($).

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