:clipboard: This app highlights search text within a string. It renders HTML on the screen via a variable and manipulates that HTML based on a query, a regular expression and regex replace logic.
TypeScript
β‘ Angular Regex Project
This app highlights search text within a string. It renders HTML on the screen via a variable and manipulates that HTML based on a query, a regular expression and REGEX replace logic.
Note: to open web links in a new window use: ctrl+click on link
A regular expression is "A sequence of characters that forms a search pattern, mainly for use in pattern-matching with strings, or string matching, i.e. βfind and replaceβ-like operations."
Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.
π» Code Examples
extract from app.component.ts file
exportclassAppComponent{privatecontent: string;publicquery: string;publicconstructor(){// tslint:disable-next-line: max-line-lengththis.content='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent a quam ornare bibendum; ligula; a, rhoncus; ligula. Etiam; aliquet, justo; sollicitudin; imperdiet; luctus, nulla justo; sodales; mi, sit; amet; semper; nisl; velit; vel; massa. In; hac; habitasse; platea; dictumst';}// if no text in query then just returnpublichighlight(): string{if(!this.query){returnthis.content;}/*The regular expression looks for all case insensitive occurrences of this.query. Take the matches and wrap them in HTML tags with the CSS class name that we created.*/returnthis.content.replace(newRegExp(this.query,'gi'),match=>{return'<span class="highlightText">'+match+'</span>';});}}
π Features
text that matches what is being searched for is highlighted. The query is case-insensitive.
π Status & To-Do List
Status: Working. Passes basic Jasmine tests
To-Do: This could be used as a sand-box to try out other angular functionality. Regex could be expanded to search for numbers and highlight them in a different colour etc.