Tag: regex

passing variable in regular expression in javascript for string match

Suppose i need to use string match in javascript to get a result equivalent to the LIKE operator. WHERE “str1″ LIKE ‘%str2%’; The below match expression works. var str1=”abcd/pqrst”; var str2 = “pqr”; if(str1.match(/^.*pqr.*/)){ //do something}; But i need to pass a variable instead of pqr, something like the below statement.Please help. //Wrong var re […]

Return specific part of string in Javascript

I would like to manipulate this following string: 20_hbeu50272359_402_21 so that I am left with only hbeu50272359 is there a way to how could I delete those outer numbers? Not sure how to go about it. The strings may also take this format: hbeu50272359_402_21

Implement date Format similar to Java/C# (like “dddd/mm/yy”)

Overall I want to implement formatting function for JavaScript date values that accept strings similar to Java/C# formatting: “dddd/mm/yy” and like. My current problem is scoped to – I have a string with ‘d’s. The string can be like ‘d/dd/ddd/dddd’ ‘dd/d/ddd’ ‘dddd ddd /dd’ the gaps between d’s can be single/more white-spaces or :(colon) or […]

find and replace tokens in javascript

I have to do something like this string = ” this is a good example to show” search = array {this,good,show} find and replace them with a token like string = ” {1} is a {2} example to {3}” (order is intact) the string will undergo some processing and then string = ” {1} is […]

Where can I find a RegEx visualisation tool for JavaScript?

I’m debugging and commenting someone else’s JavaScript code at the moment but there’s some fiendish RegEx in it. While my knowledge of RegEx is fairly good, it would help to have a reliable visualiser to show each RegEx string as a railroad diagram or something similar. I found a plug-in for Eclipse, for use with […]

Why my Regex let me write “,” (Comma)?

I have a JavaScript regex function code above: function AcceptDigits(objtextbox) { var exp = /[^\d{1,3}]/g; objtextbox.value = objtextbox.value.replace(exp, ”); }; Always that I write a any special character the function remove this character for Example: I write 45656654@ and this function clean the “@” like that 45656654. My problem The problem is that when I […]

Poorly performing regex

I’ve a really poorly performing piece of regex, currently it makes Firefox, Chrome and IE hang for a period of time. Here’s the reg-ex: ^([a-zA-Z0-9]+[/]?)+[a-zA-Z0-9]+$ It’s kind of a url matcher, but should only match the requested path (not starting with or ending with a slash). Valid examples: Segment Segment/Segment segment/segment/Segment (etc) Invalid examples: /Segment […]

Javascript – Regex finding multiple parentheses matches

So currently, my code works for inputs that contain one set of parentheses. var re = /^.*\((.*\)).*$/; var inPar = userIn.replace(re, ‘$1′); …meaning when the user enters the chemical formula Cu(NO3)2, alerting inPar returns NO3) , which I want. However, if Cu(NO3)2(CO2)3 is the input, only CO2) is being returned. I’m not too knowledgable in […]

Remove div and its content from html string

Lets say i have a string like this: <div id=”div1″></div> <div class=”aClass” id=”div2″> <div id=”div3″ class=”anotherClass”></div> <div id=”div4″ /> </div> <div id=”div5″></div> I want to remove div2 from the string and everything inside that div So i got a string like this <div id=”div1″></div> <div id=”div5″></div> I thinking something like using regex to find the […]

Regex and JavaScript String With a Dollar ($) at the end

In answering this PHP question: regex – preg_replace string, I came across something in Javascript I didn’t understand. Given the following: var s = “abc1!?d$”; alert(s.replace(/\W+/, ” “)); I am alerted: abc d$ Why is it not stripping out the last dollar?