Tag: regex

Check if string is repetition of an unknown substring in javascript

I try to solve the same problem in javascript with regexp mentioned here: Check if string is repetition of an unknown substring I translated the regex in the first answer to Javascript: ^(.+){2,}$ But it does not work as I expect: ‘SingleSingleSingle’.replace(/^(.+){2,}$/m, ‘$1’) // returns ‘e’ instead of exptected ‘Single’ What am I overlooking?

Add section title to each line using regex

I have a text file in the following format [Section1] property1 = value1 property2 = value2 [Section2] property1 = value1 property2 = value2 An example [Section foo] foo = 1 bar = “whatever” Is there any way I can add the section title to each line using regex like so Section1: property1 = value1 Section1: […]

Get number of class by part name

I have the element below: <ul id=”towns”> <li class=”clearfix odd 516_499_132_0_0 town132″>…</li> </ul> I need extract the class “town132” for one parameter, but the number – 132 for example – is variable. How do I for extract? $(‘#towns’).children(‘li’).each(function() { parameter = $(this). // ??? });

Regex Matching Issue

I want to validate an input of format “AB1234” where first two characters must be Alpha (A-Z) and remaining must be numbers. My current regex is validating the input “AB123A” which is incorrect. What is missing my current regex? ^[a-zA-Z]{2}\d{1,6}

js: special char in regex

in my regular expression, I tried to remove all “{” and “}”s from a string. Pushing the script with packer/minimizer scripts, breaks them. That’s why I’d like to know about a better and more compatible way of writing: mystring.replace(/\{/g,””);?

Regular Expression always returns true

I have following regular expression in JQuery. It always returns true. var reg = new RegExp(“[a-zA-Z0-9 ,]+”); var key = $(‘#keyId’).val().trim(); if (key.match(reg)) { $(“#TitleError”).hide(); } else { $(“#TitleError”).text(“special characters not allowed!!”).show(); } It returns true for everything, for example “ABCD, ^&&&^&” should be false, it returns true.

Regex javascript allow only numbers, alphabets and underscore

I’m using this code to prevent people from puting spaces between alphabets and numbers. I would lilke to modify the code so that only alphabets, letters and numbers can be typed. This is what i’m doing: replace(/\sg, ”) oin javascript Can anyone help me as to how to costrucrt the regex expression so that only […]

How to get JSON data from the text?

I have a string, something like this: sometext{“points”:{“point”:[{“lat”:”55.68705772049725″,”lon”:”21.127218855544925″},{“lat”:”55.68714472465217″,”lon”:”21.127141742035747″},{“lat”:”55.6871934235096″,”lon”:”21.12712439149618″},{“lat”:”55.68733625113964″,”lon”:”21.127151465043426″},{“lat”:”55.68751168437302″,”lon”:”21.12717761658132″},{“lat”:”55.687646800652146″,”lon”:”21.127120200544596″},{“lat”:”55.68781033158302″,”lon”:”21.127034788951278″},{“lat”:”55.687981490045786″,”lon”:”21.12691568210721″}]}}sometext Is it possible with regex extract JSON in Javascript? P.S. I get a lot of data and joining it in to the string. So i need to get json from that string as there can be the end of firs object and the beginning of other

Regex splitting on parenthesis, first uppercase, and digits

Right now, I have the user inputting a chemical formula, say Cu(NO3)2, and splitting the elements and numbers into an array. This code works for all formulas except for those with parentheses. (which actually came from a user-submitted thread on here.) var userArray=userIn.replace(/\d+/g, ‘~$&’).split(/(?=[A-Z])|~/); The reason I used replace then split was to ensure that […]

Javascript removes letters duplicates and sequence in string

There are many posts like this and I have found a few solutions but there are not perfect. One of them: “aabbhahahahahahahahahahahasetsetset”.replace(/[^\w\s]|(.+)\1+/gi, ‘$1’) The results is: abhahahahahahaset I want to get result: abhaset How to do this ?