News Articles regex

How to convert this Regex with lookbehind so it works in Safari?

This Regex works fine in Chrome and Firefox let regexp = new RegExp(`^${searchTerm}|(?<=s)${searchTerm}`, ‘gi’) but unfortunately Safari complains SyntaxError: Invalid regular expression: invalid group specifier name It looks like Safari doesn’t support the lookbehind, but how can I transform it so I get the indices of the searchTerm without the whitespaces? let regexp = new […]

Regex pattern to count a certain occurrences

I am trying to match a word which have either space before it or after it or both . var sample = " test-abc -test# @test _test [email protected] test test " Like in the above case the first ‘test’ will count as it has a space before it, the next will not count as it […]

JS Regex – String with at least 6 characters, one digit and one number. No whitespaces, no special characters, and only letters and digits [duplicate]

I need a Regex expression for words with at least 6 characters only letters and digits at least one digit at least one letter I have been testing this one /^((?=.*d)(?=.*[a-zA-Z]))w{6,}$/ but fails with _1234aa const re = /^((?=.*d)(?=.*[a-zA-Z]))w{6,40}$/; const password1 = “_1234aa”; console.log(re.test(password1)); // should be false const password2 = “1234aA”; console.log(re.test(password2)); // works […]

How Regex.exec() can get a code injection?

I got, from a security report, a code injection issue: This could enable an attacker to inject and run arbitrary code. The attacker can inject the executed code via user input const parseUrl = url => { const re = new RegExp(/^/+(w+)/*/); const [_, service] = re.exec(url); return service; }; I could not understand how […]

Sanitise weird unicode characters in JS [duplicate]

I do not know what to search on Google or how to ask you better than just showing the example. Let’s say you have a comment section. And some user is posting this kind of comment: (I will add a lot of fake spaces because this goes over the text; check it out in the […]

A maximum of 10 digits can be used as an integer, with two decimal places [closed]

I am pretty new about regex validation. So I would like to asking a maximum of 10 digits can be used as an integer, with two decimal places reserved. valid numbers; 45353.45 12.2 123212465432.56 invalid number; 56.787 1.9897646 Source: Vue – Stack Overflow

Adding mutiple values in regex for pattern match

I am trying to add multiple strings in regex and then test them against certain value to see if that string exists. var pattern = new RegExp("^"+"ABC","i"); var patternMatch= pattern.test(SomeArray[0]); Now I require to add multiple string values in regex and then test if it exists in array. Something like this : var pattern = […]

Regex CSS Style Attribute replacements

I want to selectively replace an attribute value within a CSS Style string… The string: "font-size:13pt;font-weight:600;line-height:140%;padding-top:10px;font-family:’Open Sans’, ‘Droid Sans’, Arial, sans-serif;color:#7f1a52;padding-bottom:5px;vertical-align:middle;background-color:; text-align:center;align:center;" I want to selectively replace the "center" in text-align and align, but NOT the middle in vertical-align. So far I have string.replace(/(?<=align:)(.*?)(?=;)/g,"right") but that will also replace the middle in vertical align. How […]

Javascript/HTML DOM: How do I check if text user input equals to string in if statement

I am trying to check in my if statement if the text the user inputs from my html form equals to the string I indicate when submit is clicked. I tried using regular expressions to check if the text is a string, but not sure if done properly. Both if statements end up going straight […]

Regular expression to check if digit is at specific place

I’m new to the regular expression I need a regular expression that validates an ID number the ID number has 9 digits, the fifth digit on the ID number should be 1 or 2 Source: JavaSript – Stack Overflow

Match not found when using the RegExp constructor [duplicate]

I’m about to build a regular expression that matches different strings from an array. When testing, the following works as intended and returns the matched groups. let str = ‘5cm’; let re = /([d,./[]]+)(cm)$/; console.log(str.match(re)); However, when using the RegExp() constructor for the same regular expression, null is returned. let str = ‘5cm’; let re […]

How to exclude some characters and include some [closed]

I have a string like let str= "abc,123,xyz" and I want to split them like this abc,132 and xyz Source: JavaSript – Stack Overflow

Javascript: GUID: RegEx: string to GUID

I have a textbox that a user can paste into using Ctrl+V. I would like to restrict the textbox to accept just GUIDs. I tried to write a small function that would format an input string to a GUID based on RegEx, but I can’t seem to be able to do it. I tried following […]

Regex not validating domain names correctly in JS [duplicate]

I am using such regex "/^w[w-]{1,61}w.[A-Za-z]{2,6}(?:.[A-Za-z]{2,6})?$/" and if I use the domain like www.stayrare.machine or www.stayrare.machines then I get error. Source: JavaSript – Stack Overflow

RegExp `Range out of order in character class`

I have this RegExp: [u{1f300}-u{1f5ff}u{1f900}-u{1f9ff}u{1f600}-u{1f64f}u{1f680}-u{1f6ff}u{2600}-u{26ff}u{2700}-u{27bf}u{1f1e6}-u{1f1ff}u{1f191}-u{1f251}u{1f004}u{1f0cf}u{1f170}-u{1f171}u{1f17e}-u{1f17f}u{1f18e}u{3030}u{2b50}u{2b55}u{2934}-u{2935}u{2b05}-u{2b07}u{2b1b}-u{2b1c}u{3297}u{3299}u{303d}u{00a9}u{00ae}u{2122}u{23f3}u{24c2}u{23e9}-u{23ef}u{25b6}u{23f8}-u{23fa}u{200d}] When I’m using this RegExp on https://regex101.com it’s working. But when I’m using this RegExp in JavaScript or Dart, I’m getting Range out of order in character class Error. I’m really sure that this is a String escaping error, but I’m not able to find the problem. I already tried […]

remove items in array from a string

I have a string and an array that look like the following: const str = ‘Liverpool Chelsea Barca’; const englishTeams = [‘Liverpool’, ‘Chelsea’] I want to remove both items in the array from the string, but not sure exactly the best way to do this. I know I can do the following but I need […]

Replace strings ignoring spaces and newlines between them

Imagine we have a text like : batman superman I want to replace batman superman with hitman spiderman: hitman spiderman For instance: batman not valid superman will not get replaced with hitman spiderman, because we have other characters (not valid) in between them. Note the spaces/newlines should remain as such. How to come up with […]

Match and replace the string but not the whitespace/newline

I have a regex like this: a[sS]*b which matches the following: a b I want to match a b and ignore newlines in them. Basically I want to replace a,b with c, d, without replacing the new lines. The regex that I have replaces the new lines too. How to ignore them? Or in other […]

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.