Tag: regex

Exclude array of words from regex match in Javascript

I am given two variables, a string [var str] and an array of words [var exceptions]. I am replacing every middle character of every word longer than 3 letters with an asterisk using the following regular expression: var edited = str.replace(/\B\w\B/g, ‘*’); For example, the string “This is an example of what I am doing” […]

Regular Expression to Check numeric field doesn't have /or have all same digits

I have to validate a field. The field should be numeric, should not contain all the same digits (like 555555, 111111) . I need a regular expression to check this. I need to know how can I check if it contains all same digits. tried few solutions like following but didn’t worked these three didn’t […]

Mobile number field accepts invalid number too

Here is a validation code that I use to validate mobile number. If I enter a mobile number with alphabets or any invalid number, it accepts that too. Can someone suggest what is wrong in this? Thanks var g=document.getElementById(‘mobile’).value; if (g.length == 0 ) { alert( “Please Enter Mobile Number.” ); formn.mobile.focus(); return false; } […]

Regular expression to check word and word length

In JavaScript I am using the regular expression /^([a-z]){3}^(foo)/i to try and match a specific word and the word’s length. The regex should also be case insensitive. So I added an i modifier on the end. What I think this says it should do is ^ : Start at beginning of string ([a-z]){3} : Match […]

How to get float value from string using regex?

I have a string value that will look like this: “ZAR 200.15” Using regex, how can I extract the float value to do calculations with? For some context, I’m using javascript to access an HTML element’s value like this: var amountDueString = document.getElementById(“amountDue”).innerHTML; Then I need to use regex to only get the float value. […]

Trying to escape a string to use as a jQuery selector

I have the following code, which I stole from another SO question, $(‘#’+’^`test’.replace(/[!”#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, “\\\\$&”)) which produces the following error. Uncaught Error: Syntax error, unrecognized expression: #\\^\\`test(…) I just have some IDs with crazy characters, like ^ and `, that I need jQuery to not choke on. I don’t get why that error is happening, because […]

Check if JSON LIKE string is valid – JS regex

I have CLI app where user provide a JSON. I need to check if JSON is valid. I found somehting like this may work great: function isJsonValid(str) { try { JSON.parse(str); } catch (e) { return false; } return true; } But while I debug my app i noticed that there is a little problem […]

Split string by all spaces except those in parentheses

I’m trying to split text the following like on spaces: var line = “Text (what is)|what’s a story|fable called|named|about {Search}|{Title}” but I want it to ignore the spaces within parentheses. This should produce an array with: var words = [“Text”, “(what is)|what’s”, “a”, “story|fable” “called|named|about”, “{Search}|{Title}”]; I know this should involve some sort of regex […]

Javascript Regex – Capture Only Unescaped Patterns

Given a string in Javascript like {include} anything in {curly braces} but not when they are escaped like {this} and work for any position of the {brace} I want to retrieve include curly braces brace I have been trying to find a way, but without look behind I am stumped. The most important thing is […]

Capitalize the beginning of each sentence

I would like to capitalize the beginning of each sentence. I have the following code from other question: function applySentenceCase(str) { return str.replace(/.+?[\.\?\!](\s|$)/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } But if i don’t put a dot for a last sentence, it doesn’t work properly. For example: for string “THIS IS THE FIRST […]