Query check using RegExp

Specific results can be obtained in loklak using different combinations of type associated with query (e.g. from:user, @user, #query). For identifying the requirement of user through query and maintaining the flow of API requests to the loklak server, RegExp is being used. Through this blog post, I’ll be discussing about its use in the project.

Using RegExp in Services

Patterns for all possible query types has already been defined in reg-exp typescript file inside utils/ folder. We would need to import these expressions to check type of query anywhere inside the codebase.

The query (q) arguments of search endpoints of api.loklak depends on type associated with query i.e. argument for from:user would be different from that of @user. To keep check on the same, I have used RegExp.

if ( hashtagRegExp.exec(query) !== null ) {
       // Check for hashtag query
       jsonpUrl += ‘&q=%23 + hashtagRegExp.
       exec(query)[1] +  + hashtagRegExp.exec(query)[0];
   } else if ( fromRegExp.exec(query) !== null ) {
       // Check for from user query
       jsonpUrl += ‘&q=from%3A’ + fromRegExp.exec(query)[1];
   } else if ( mentionRegExp.exec(query) !== null ) {
       // Check for mention query
       jsonpUrl += ‘&q=%40 + mentionRegExp.exec(query)[1];
   } else if ( nearRegExp.exec(query) !== null ) {
       // Check for near query
       jsonpUrl += ‘&q=near%3A’ + nearRegExp.exec(query)[1];
   } else {
       // for other queries
       jsonpUrl += ‘&q=’ + query;


A similar architecture has been used in UserService and SuggestService.

Using RegExp in info-box

A new feature has been added in loklak to provide RSS and JSON feed through api.loklak.org. It requires a query check which is then passed through anchor tag link to provide RSS and JSON feed based on the query and type associated with it. It is being included under info-box and the query check is done in ngOnChanges() which means it will keep updating itself with updating query.

   this.store.select(fromRoot.getQuery).subscribe(query => this.stringQuery = query.displayString);
   if ( hashtagRegExp.exec(this.stringQuery) !== null ) {
       // Check for hashtag this.stringQuery
       this.queryString = %23 + hashtagRegExp.
       exec(this.stringQuery)[1] +  + hashtagRegExp.exec(this.stringQuery)[0];
   } else if ( fromRegExp.exec(this.stringQuery) !== null ) {
       // Check for from user this.stringQuery
       this.queryString = from%3A + fromRegExp.exec(this.stringQuery)[1];
   } else if ( mentionRegExp.exec(this.stringQuery) !== null ) {
       // Check for mention this.stringQuery
       this.queryString = %40 + mentionRegExp.exec(this.stringQuery)[1];
   } else {
       // for other queries
       this.queryString = this.stringQuery;


A similar architecture is followed throughout the project in different features implemented so far. It would not be easy to provide each of those implementation in one blog. For actual implementations, please go through the codebase.


Continue ReadingQuery check using RegExp