JavaScript Regular Expression fails to match non-white characters

What is the reason for this?

Illustration 1

re = new RegExp('\\$\\[(.+)\\]\\(([S,M,L]),([L,C,R])\\)', 'g')
'$[sds](S,L)'.replace(re, function(a,b,c,d) { 
        console.log(a,b,c,d); return 'test'; 
})

yet this fails to work

Illustration 2

re = new RegExp('\\$\\[(\S+)\\]\\(([S,M,L]),([L,C,R])\\)', 'g')
'$[sds](S,L)'.replace(re, function(a,b,c,d) { 
     console.log(a,b,c,d); return 'test'; 
})

The key difference between the two is that Illustration 1 includes .+, whereas in illustration 2 it's \S+.

The expectation is for \S+ to identify the sds within the brackets, operating similarly to .+

Answer №1

Make sure to properly escape the backslash when using \S:

new RegExp('\\$\\[(\\S+)\\]\\(([S,M,L]),([L,C,R])\\)', 'g')

You can see it in action in this fiddle: http://jsfiddle.net/rtXLN/

One of the comments pointed out that there is a mistake in your Regex pattern:

new RegExp('\\$\\[(\\S+)\\]\\(([SML]),([LCR])\\)', 'g')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Threejs JSONLoader and OOP to easily add models to a scene post-loading

I am working on incorporating the THREE JSONLoader into a "Scenemanager" Object that manages the addition and removal of objects and models. My main goal is to deepen my understanding of OOP, JS, and Threejs. Within App3D (which oversees the scene), I cal ...

Another problem with CORS again?

My rails application uses the host http://myhost.dev to search for music through the vk.com API. The API provides a search method called audio.search. Below is the code snippet from my search.js.erb file which is executed via a remote link to the search ac ...

Using an In-Memory Isolated Neo4J Database to Test NodeJS Applications

Can anyone suggest a method to quickly create small in-memory Neo4J database instances for testing NodeJS code with Jest? ...

React: The art of organizing components. Exploring Composition and Render props

Are you prepared for this long question? I've recently delved into working with react-query and noticed that a significant amount of code needs to be duplicated for each component utilizing the useQuery hook. For instance: if(query.isLoading) { re ...

What steps should I follow to modify information in my JSON file through Postman?

Is there a way to update data in a JSON file using the patch method in Postman? I am looking for the syntax to use when selecting the patch method and clicking send, so that my data gets updated in the JSON file. const express = require("express&quo ...

The error message "404 Not Found" was triggered when attempting to deliver the

Here is the server-side code snippet that I am using: var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendfile( ...

Exploring the filtering capabilities of Firebase using Vue

I am currently trying to enhance the search functionality in my firebase database to enable users to easily locate the product they are looking for. My current approach involves matching the search query, stored in this.searchText, with the product titles ...

Streamline Access to Zimbra Server Automatically

Currently, I am creating a webpage with a login feature at www.newpage.com. Here is the code snippet for reference: <form name="login"> Username<input type="text" name="userid"/> Password<input type="password" name="pswrd"/> <input ty ...

Objects become visible over a sphere only when they are positioned behind it

I am currently working on a project to create a dynamic rotating globe with points of interest marked on it. The globe rotates, and I want the points of interest to be visible whenever they are in view. However, I am facing an issue where the points of in ...

jQuery tooltip anchored to the left side of the screen, ignoring all attempts to adjust its position

I have a jQuery tooltip that is supposed to hover over the table header, but it is displaying on the far left side of my screen with no formatting - just black text. I have attempted to use the placement and position jQuery attributes, but they do not seem ...

Interacting with YouTube Data API without requiring user input

I'm currently developing a music website that enables users to create YouTube playlists. Initially, I experimented with JavaScript: https://developers.google.com/youtube/v3/code_samples/javascript The procedure involves an initial authorization ste ...

Utilizing PHP to create images and sending them as attachments in Discord using a URL

I am attempting to upload an image from a link using my Discord bot. The image is created with PHP utilizing Image Processing and Generation, and the output is in PNG format. However, when I try to attach the image to my bot's message using the code ...

Methods for automating the clicking of a hyperlink within Bootstrap tabs programmatically

I am attempting to programmatically trigger a click event on a specific href link within Bootstrap tabs. For reference, here is the link to the codepen: linkToCodePen <ul class="nav nav-tabs" id="myNavTabs"> <li class="active">&l ...

Is there a way to determine if npm packages are accessing and misusing my system's environment variables?

Apologies if this seems nonsensical. But including a code snippet like this: // to illustrate I'm utilizing a source from https://www.npmjs.com/package/got got.post(maliciousUrl, {json: process.env}) Is it enough to leak environment variables to an u ...

How can you use jQuery to display an image when hovering over text?

Looking for a tutorial or script that displays an image when hovering over HTML text with the mouse? ...

Issue with div element not stretching to 100% width

I am currently working on a fluid layout design where the template includes a header, menu, and body section. <div class="w100 h100"> <div id="headerBox" style="height: 10%;" class="w100"> <div style="width: 80%;" class="lfloat ...

What benefits does using Object.create(null) offer in comparison to utilizing a Map?

I've noticed that some developers prefer using code that looks like const STUFF_MAP = Object.create(null); It seems that STUFF_MAP is intended to be used like a map, hence the choice of using Object.create(null) over {} (to avoid conflicts with prope ...

What is the process of establishing a form in Angular and making modifications to the form without saving it?

Hey everyone, I've created a form that should open when clicking on the "Edit Button". However, when I try to edit the form and click on the "Save" button, it doesn't save the data. Can someone please help me with this issue? Code // Here is t ...

Exclusively for Numerical Input with Number Keys

A customer on a website requires a Zip Code field that only accepts 5-digit numbers and triggers the numeric keypad on iOS and Android devices. It should also be compatible with external number pads on laptops. I've tried various approaches like keyC ...

Obtain the count of a two-value array from a Laravel controller and pass it to JavaScript

I need help with a query that calculates the count based on conditions for genders "male" and "female". $users[] = User::whereIn('gender',array('male', 'female'))->count()->get(); return view('/',comp ...