1
>
/*---------------------------------------------------------------------------------------------
jsonc.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
// First group matches a double quoted string
7
>
// Second group matches a single quoted string
8
>
// Third group matches a multi line comment
9
>
// Forth group matches a single line comment
10
>
// Fifth group matches a trailing comma
11
>
const regexp = /("[^"\\]*(?:\\.[^"\\]*)*")|('[^'\\]*(?:\\.[^'\\]*)*')|(\/\*[^\/\*]*(?:(?:\*|\/)[^\/\*]*)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))|(,\s*[}\]])/g;
12
>
13
>
/**
14
>
* Strips single and multi line JavaScript comments from JSON
15
>
* content. Ignores characters in strings BUT doesn't support
16
>
* string continuation across multiple lines since it is not
17
>
* supported in JSON.
18
>
*
19
>
* @param content the content to strip comments from
20
>
* @returns the content without comments
21
>
*/
22
>
export function stripComments(content: string): string {
23
>
return content.replace(regexp, function (match, _m1, _m2, m3, m4, m5) {
24
>
// Only one of m1, m2, m3, m4, m5 matches
25
>
if (m3) {
26
// A block comment. Replace with nothing
27
return '';
29
// Since m4 is a single line comment is is at least of length 2 (e.g. //)
30
// If it ends in \r?\n then keep it.