Javascript Regex not working with Node [duplicate] - javascript

This question already has answers here:
Regex works on browser but not in Node.js
(2 answers)
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I have the following regex that works when used on the chrome browser:
file.match(/(?<=__\()(.*)(?=\))/g);
But it seems to throw an error when using Node on my machine:
SyntaxError: Invalid regular expression: /(?<=__\()(.*)(?=\))/: Invalid group
Any ideas why this might be? Also I am trying to match string that appear in groups like this:
__("string to match");
const file = "__('string to be extracted');";
const strings = file.match(/(?<=__\()(.*)(?=\))/g);
console.log(strings);

Related

Why JSON.parse for one backslash returns two backslashes, but not four ones [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
How to deal with backslashes in json strings php
(6 answers)
Closed 3 years ago.
I have the string with backslashes:
const str = 'E:\\music\\post'; // It represents "E:\music\post" text in HTML
JSON.stringify(str) returns "the same" string '"E:\\music\\post"'. Why not '"E:\\\\music\\\\post"'?
JSON.parse('"E:\\music\\post"') will throws the error: Uncaught SyntaxError: Unexpected token m in JSON at position 4.
JSON.parse('"E:\\\\music\\\\post"') works OK.
But another strange thing is that JSON.parse(JSON.stringify("E:\\music\\post")) works well. Why?
That is the better workaround for it? I want to save the string to json file, and later to create an object from the json file.

Javascript regex throwing syntax error in edge [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
I have a simple regex I'm using and that works perfectly in chrome but edge throws a syntax error, ths is the line :
var html=text.match(/^<div.+\/div>$/ims);
I don't see the problem.
Because /s flag is not supported, use:
var html=text.match(/^<div[\s\S]+\/div>$/im);
Basically you want to match all characters with a new line character
You can this regex please:-
text.match(/^<div>.+\n*.*<\/div>/)

How can I match filenames that start with a given string using a RegEx in JavaScript? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I am looking for a JavaScript RegEx that matches all woff and woff2 files that start with a particular String, i.e. MyFont.
The following examples should match:
MyFont-bold.woff
MyFont-light.woff2
The following example should not match:
GoogleFont-bold.woff
SomeOtherFont-light.woff2
MyFont-something.css
^MyFont.*.woff2?$/ would work.
var regex = /^MyFont.*.woff2?$/
console.log(regex.test('MyFont-bold.woff'))
console.log(regex.test('MyFont-light.woff2'))
console.log(regex.test('GoogleFont-bold.woff'))
console.log(regex.test('SomeOtherFont-light.woff2'))
console.log(regex.test('MyFont-something.css'))

JavaScript runtime error: Invalid character using JSON.parse [duplicate]

This question already has answers here:
jQuery.parseJSON single quote vs double quote
(4 answers)
Closed 7 years ago.
The following throws "JavaScript runtime error: Invalid character":
var opts = "[{'UOM':'E','Description':'E - Each'},{'UOM':'C','Description':'C - Per 100'},{'UOM':'M','Description':'M - Per 1000'}]"
var options = JSON.parse(opts);
What invalid character is in my JSON string? Thanks
You need to replace the apostrophes with escaped quotation marks (\").

JavaScript Regular Expression nothing to repeat [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 7 years ago.
I'm using the following JavaScript code to create a regular expression to match a UK mobile number:
new RegExp("(\+44|0)7\d{9}", 'g');
However, I get an error in the console log saying:
Uncaught SyntaxError: Invalid regular expression: /(?:+44|0)7d{9}/:
Nothing to repeat
Similar questions on StackOverflow point to a missing escaped character, but mine seem to be fine.
I have also tried without the global flag.
Help would be greatly appreciated.
You can create RegExp by using short method
var a = /(\+44|0)7\d{9}/g
Its must works good)

Categories