Переглянути джерело

Escape strings for use in regex search. (#23729)

Michael Herzog 3 роки тому
батько
коміт
6580a6e6d2
2 змінених файлів з 14 додано та 2 видалено
  1. 7 1
      docs/index.html
  2. 7 1
      examples/index.html

+ 7 - 1
docs/index.html

@@ -350,6 +350,12 @@
 
 		}
 
+		function escapeRegExp( string ) {
+
+			return string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // https://stackoverflow.com/a/6969486/5250847
+
+		}
+
 		function updateFilter() {
 
 			let v = filterInput.value.trim();
@@ -367,7 +373,7 @@
 
 			//
 
-			const regExp = new RegExp( filterInput.value, 'gi' );
+			const regExp = new RegExp( escapeRegExp( v ), 'gi' );
 
 			for ( let pageName in pageProperties ) {
 

+ 7 - 1
examples/index.html

@@ -249,6 +249,12 @@
 
 		}
 
+		function escapeRegExp( string ) {
+
+			return string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // https://stackoverflow.com/a/6969486/5250847
+
+		}
+
 		function updateFilter( files, tags ) {
 
 			let v = filterInput.value.trim();
@@ -264,7 +270,7 @@
 
 			}
 
-			const exp = new RegExp( v, 'gi' );
+			const exp = new RegExp( escapeRegExp( v ), 'gi' );
 
 			for ( const key in files ) {