Remove leading and trailing quotes from user-submitted strings in VueJS
P粉076987386
P粉076987386 2024-03-26 22:25:27
0
1
406

I have a VueJS application that contains forms that are pre-populated with data. The textarea element contains some data as shown on the UI

NOTE: (Sorry, I don't have enough rep points to attach a screenshot yet):

Screenshot of quoted UI text area element

I cannot modify the way the backend provides data.

So, is there a good way to remove the first and last quotes from a string? I don't want to touch the inner text in case the user adds quotes in it.

The expected output of

'Hello World,welcome to "fooWorld!"' should be Hello World,welcome to 'fooWorld!'

Here is a real-life example of how I use data in a SPA:

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

P粉076987386
P粉076987386

reply all(1)
P粉930534280

How to remove opening and closing quotes using fast regex?

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

The first regular expression looks for the beginning of the string (represented by ^), then for quotes. The second regex looks for quotes and then for the end of the string (the end of the string is represented by $).

You can also do this:

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

This looks for the beginning of the string (^), followed by a ", but the ? makes it optional. Then the brackets capture . , which means match anything. The ? before the end of the bracket means stop matching as soon as possible. Then it says to look for quotes, but the next question mark says "maybe" and then looks for the end of the string ($).

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template