![]() substr expects 2 arguments, where the first is the offset from which the substring starts, the second is its length. Strings are zero-indexed, so the last char is the charAt(str.length -1). If they are, I'm using substr to cut off those first and last chars. Here, I'm checking if the first and last char in the string are double quotes. However, in this last case, it's far safer, faster, more maintainable and just better to do this: if (str.charAt(0) = '"' & str.charAt(str.length -1) = '"')Ĭonsole.log(str.substr(1,str.length -2)) Ah well, but if this is what you want/need, please do read on: My guess would be that the lookaround assertion causes the previous expression to fail as soon as the engine determines there is no " at the end of the string. I've tried this regex, along side the one above (with lookahead assertion) and, admittedly, to my surprize found this one to be slightly slower. You may have noticed I've omitted the g flag (for global BTW), because since we're processing the entire string, this expression only applies once.Īn easier regex that does, pretty much, the same thing (there are internal difference of how the regex is compiled/applied) would be: someStr.replace(/^"(.+)"$/,'$1') Īs before ^" and "$ match the delimiting quotes at the start and end of a string, and the (.+) matches everything in between, and captures it. The replacement is done in the same way as before: we replace the match (which includes the opening and closing quotes), with everything that was inside them. "$: matches that ending quote, but does not capture it.(?="$): the positive lookahead is much the same as above, only it specifies that the " must be the end of the string ( $ = end). ![]() (.+(?="$)): matches (and captures) everything, including double quotes one or more times, provided the positive lookahead is true.If the string does not start with a ", the expression already fails here, and nothing is replaced. ^": matches the beginning of the string ^ and a ".With input remove "foo" delimiting ", the output will remain unchanged, but change the input string to "remove "foo" delimiting quotes", and you'll end up with remove "foo" delimiting quotes as output. Or this for double and single quotes: str.replace(/^(.+(?=$))$/, '$1') If the quotes are always going to be at the begining and end of the string, then you could use this: str.replace(/^"(.+(?="$))"$/, '$1') logs remove only foo delimiting " replaces "string with" with -> string with. Str = 'remove only "foo" delimiting "' //note trailing " at the end You'll have to use lookaround assertions: var str = 'remove "foo" delimiting double quotes' Ĭonsole.log(str.replace(/"(+(?="))"/g, '$1')) If you're trying to remove the quotes around a given string (ie in pairs), things get a bit trickier. If you omit this, you'll only replace a single char. This tells JS to apply the regex to the entire string. +: one or more quotes, chars, as defined by the preceding char-class (optional).you can replace this with " to only match double quotes. is a character class, matches both single and double quotes.(if your goal is to replace all double quotes). ![]() Assuming: var someStr = 'He said "Hello, my name is Foo"' Ĭonsole.log(someStr.replace(/+/g, ''))
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |