Reputation: 23
Query is similar to the one mentioned in
New to typescript and front end dev. Using Angular form to take input from user which may contain regex.
Example:
this.expressions is actually a input from form multiple expressions can be passed seperated by new line character.
this.rewrite_object = []
this.expressions = "s|.*|{{ date | replace('-', '') }}\"/apps/data/{{ date | replace('-', '/') }}"
this.rewrite_array = this.expressions.split('\n')
console.log("******************Print after split******************")
console.log(this.rewrite_array)
if (this.rewrite_array.length > 0) {
for(var i = 0;i<this.rewrite_array.length;i++) {
if (this.rewrite_array[i]) {
console.log("******************Print before adding to array******************")
console.log(this.rewrite_array[i])
this.rewrite_object.push({ "expression": this.rewrite_array[i] });
console.log("******************Print rewrite object******************")
console.log(this.rewrite_object)
}
}
}
This is what I get in output. escape character vanish.
******************Print after split******************
[
`s|.*|{{ date | replace('-', '') }}"/apps/data/{{ date | replace('-', '/') }}`
]
******************Print before adding to array******************
s|.*|{{ date | replace('-', '') }}"/apps/data/{{ date | replace('-', '/') }}
******************Print rewrite object******************
[
{
expression: `s|.*|{{ date | replace('-', '') }}"/apps/data/{{ date | replace('-', '/') }}`
}
]
Thought of escaping escape character itself by using \ but this fails with error during compilation.
test.ts:2:70 - error TS1136: Property assignment expected.
2 this.expressions = "s|.*|{{ date | replace('-', '') }}\\"/apps/data/{{ date | replace('-', '/') }}"
~
test.ts:2:98 - error TS1128: Declaration or statement expected.
2 this.expressions = "s|.*|{{ date | replace('-', '') }}\\"/apps/data/{{ date | replace('-', '/') }}"
~
test.ts:2:100 - error TS1002: Unterminated string literal.
2 this.expressions = "s|.*|{{ date | replace('-', '') }}\\"/apps/data/{{ date | replace('-', '/') }}"
Found 3 errors.
How can we make " work with typescript as it's. I need to save this information in DB after taking as input from customer.
Upvotes: 1
Views: 1042
Reputation: 8988
You need to escape the backslash but still also escape the double quote: to receive a \"
in the final string you have to write \\\"
within your doubl-quoted string.
The first \
will escape the second one giving you one literal \
in the resulting string. The third \
escapes the double quote so it will be included in the resulting string and not terminate your string.
this.expressions = "s|.*|{{ date | replace('-', '') }}\\\"/apps/data/{{ date | replace('-', '/') }}"
Upvotes: 1