I am having a hard time understanding how to match a certain regular expression using javascripts match() function. I have a field in a table stored in the following format:
CH-01-Feb-13-1. I want to be able to grab the date without the dashes, i.e. 01-Feb-13. I was trying to figure out how to combine with ^- or . but not sure how to do it.
So you want the regular expression?
Something like
^\w{2}-(\d{2}-\w{3}-\d{2}).*?$
You can see the explanation here: http://www.regexper.com/ Just copy and paste the expression.
Example with Javascript
var r = /^\w{2}-(\d{2}-\w{3}-\d{2}).*?$/i
var groups = "CH-01-Feb-13-1".match(r);
console.log(groups);
If you are not comfortable with Regex then you can use something like this.
var str = 'CH-01-Feb-13-1';
str = str.replace('CH-','');
str = str.split('-');
str.pop();
console.log(str.join('-'));
Related
I am trying to do something fairly simple however I have not worked extensively with Regex before.
I am trying to extract some strings out of another string.
I have the string 'value/:id/:foo/:bar'
I would like to extract each string after the colon and before slash eg:
let s = 'value/:id/:foo/:bar';
let r = new RegExp(/MAGIC HERE/);
// result r.exec(s)
I have been trying for an hour or so on this website: https://regex101.com/ but can only get as close as this:
:([a-z]+)
I also tried playing with these examples but couldn't get anywhere:
Regex match everything after question mark?
How do you access the matched groups in a JavaScript regular expression?
I want to be able to extract these parameters infinitely if possible.
My intended result is to get an array of each of the parameters.
group 1 - id
group 2 - foo
group 3 - bar
Please consider explaining the regex that can help with this I want to understand how groups are formed in the regex.
'value/:id/:foo/:bar'.match(/:[a-z]+/g)
Returns
[":id", ":foo", ":bar"]
try this:
let reg=/:(.*?)\/|:(.*?)$/g;
let reg2=/:(.*?)\/|:(.*?)$/;
let str='value/:id/:foo/:bar';
let result=str.match(reg).map(v=>v.match(reg2)[1]?v.match(reg2)[1]:v.match(reg2)[2]);
console.log(result);
"/:"
This regex , don't try to match the text between separators, but the separator '/:'.
I hope this help...
let s = 'value/:id/:foo/:bar';
s = s.split("\/\:").splice(1).map((current, index) => `group ${index+1}: - ${current}`);
console.log(s);
I'm looking for a reliable way to get function names from a string. The string values can be something like this:
let str = 'qwe(); asd();zxc()'
//or
let str = 'qwe("foo");asd(1);zxc();'
//etc.
I want to have an array
['qwe', 'asd', 'zxc']
I tried str.split(';') but how do I get rid of parenthesis and anything they can hold? Is there a regexp that will match all symbols on the left of some other symbol?
You can use this simple regex to find function names in .match()
var str = "qwe(); asd();zxc()";
console.log(str.match(/\w+(?=\()/g));
The first case it's fairly simple with regex a simple
[A-Za-z]\w+
would suffice.
on the second case it's a little bit trickier but maybe supressing the match for this
"(.*?)"
maybe a possibility
I'm trying to replace multiple occurrences of a string and nothing seems to be working for me. In my browser or even when testing online. Where am I going wrong?
str = '[{name}] is happy today as data-name="[{name}]" won the match today. [{name}] made 100 runs.';
str = str.replace('/[{name}]/gi','John');
console.log(str);
http://jsfiddle.net/SXTd4/
I got that example from here, and that too wont work.
You must not quote regexes, the correct notation would be:
str = str.replace(/\[{name}\]/gi,'John');
Also, you have to escape the [], because otherwise the content inside is treated as character class.
Updating your fiddle accordingly makes it work.
There are two ways declaring regexes:
// literal notation - the preferred option
var re = /regex here/;
// via constructor
var re = new Regexp('regex here');
You should not put your regex in quotes and you need to escape []
Simply use
str = str.replace(/\[{name}\]/gi,'John');
DEMO
While there are plenty of regex answers here is another way:
str = str.split('[{name}]').join('John');
The characters [ ] { } should be escaped in your regular expression.
I have this string:
var str = "jquery12325365345423545423im-a-very-good-string";
What I would like to do, is removing the part 'jquery12325365345423545423' from the above string.
The output should be:
var str = 'im-a-very-good-string';
How can I remove that part of the string using php? Are there any functions in php to remove a specified part of a string?
sorry for not including the part i have done
I am looking for solution in js or jquery
so far i have tried
var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("jquery12325365345423545423","");
but problem is numbers are randomly generated and changed every time.
so is there other ways to solve this using jquery or JS
The simplest solution is to do it with:
str = str.replace(/jquery\d+/, '').replace(' ', '');
You can use string replace.
var str = "jquery12325365345423545423im-a-very-good-string";
str.replace('jquery12325365345423545423','');
Then to removespaces you can add this.
str.replace(' ','');
I think it will be best to describe the methods usually used with this kind of problems and let you decide what to use (how the string changes is rather unclear).
METHOD 1: Regular expression
You can search for a regular expression and replace the part of the string that matches the regular expression. This can be achieved through the JavaScript Replace() method.
In your case you could use following Regular expression: /jquery\d+/g (all strings that begin with jquery and continue with numbers, f.e. jquery12325365345423545423 or jquery0)
As code:
var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("/jquery\d+/g","");
See the jsFiddle example
METHOD 2: Substring
If your code will always have the same length and be at the same position, you should probably be using the JavaScript substring() method.
As code:
var str="jquery12325365345423545423im-a-very-good-string";
var code = str.substring(0,26);
str=str.substring(26);
See the jsFiddle example
Run this sample in chrome dev tools
var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("jquery12325365345423545423","");
console.log(str)
I am trying to create a regular expression that would easily replace an input name such as "holes[0][shots][0][unit]" to "holes[0][shots]1[unit]". I'm basically cloning a HTML input and would like to make sure its position is incremented.
I got my regex built and working correctly using this (awesome) tool : http://gskinner.com/RegExr/
Here is my current regex :
(.*\[shots\]\[)([0-9]+)(\].*\])
and I am using a replace such as :
$12$3
this transforms "holes[0][shots][0][unit]" into "holes[0][shots][2][unit]". This is exactly want I want. However, when I try this in javascript (http://jsfiddle.net/PH2Rh/) :
var str = "holes[0][shots][0][units]";
var reg =new RegExp("(.*\[shots\]\[)([0-9]+)(\].*\])", "g");
console.log(str.replace(reg,'$1'));
I get the following output : holes[0
I don't understand how my first group is supposed to represent "holes[0", since I included the whole [shots][ part in it.
I appreciate any inputs on this. THank you.
In strings, a single \ is not interpreted as a Regex-escaping character. To escape the bracket within string literals, you have to use two backslashes, \\:
var reg = new RegExp("(.*\\[shots\\]\\[)([0-9]+)(\\].*\\])", "g");
A preferable solution is to use RegEx literals:
var reg = /(.*\[shots\]\[)([0-9]+)(\].*\])/g;
Looks like, this works:
var str = "holes[0][shots][0][units]";
var reg = /(.*\[shots\]\[)([0-9]+)(\].*\])/;
console.log(str.replace(reg,'$1'));