I got this
'','','{12345678},{87654321}','lk12l1k2l12lkl12lkl121l2lk12'
trying to match it using '(.*?)',|'(.*?)'
It successfully got my 4 chunks
''
''
'{12345678},{87654321}'
'lk12l1k2l12lkl12lkl121l2lk12'
But I am trying to use the same regex in split... it doesn't like it. :(
var str = "'','','{12345678},{87654321}','lk12l1k2l12lkl12lkl121l2lk12'";
str.split(/'(.*?)',|'(.*?)'/);
Any idea...? ugh.
Why are you using split?
You can get your four chunks with match:
var chunks = str.match(/'[^']*'/g);
Is the split() neccessary?
You could always get the information between the quotes using match().
test.match(/'(.*?)'/g)
Test it please.
Related
Actually I'm getting the arraylist from android device in node.js . But as it's in string form so I wanna convert it into an array . For that I've referred a lot of similar questions in SO but none of them were helpful . I also tried to use JSON.parse() but it was not helpful.
I'm getting societyList in form '[Art, Photography, Writing]'.Thus how to convert this format to an array?
Code:
var soc_arr=JSON.parse(data.societyList)
console.log(soc_arr.length)
use something like this
var array = arrayList.replace(/^\[|\]$/g, "").split(", ");
UPDATE:
After #drinchev suggestion regex used.
regex matches char starts with '[' and ends with ']'
This string is not valid JSON since it does not use the "" to indicate a string.
The best way would be to parse it yourself using a method like below:
let data = '[test1, test2, test3]';
let parts = data
.trim() // trim the initial data!
.substr(1,data.length-2) // remove the brackets from string
.split(',') // plit the string using the seperator ','
.map(e=>e.trim()) // trim the results to remove spaces at start and end
console.log(parts);
RegExp.match() maybe
console.log('[Art, Photography, Writing]'.match(/\w+/g))
So match() applies on any string and will split it into array elements.
Use replace and split. In addition, use trim() to remove the trailing and leading whitespaces from the array element.
var str = '[Art, Photography, Writing]';
var JSONData = str.replace('[','').replace(']','').split(',').map(x => x.trim());
console.log(JSONData);
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 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 parse a webpage and to get the number reference after <li>YM#. For example I need to get 1234-234234 in a variable from the HTML that contains
<li>YM# 1234-234234 </li>
Many thanks for your help someone!
Rich
currently, your regex only matches if there is a single number before the dash and a single number after it. This will let you get one or more numbers in each place instead:
/YM#[0-9]+-[0-9]+/g
Then, you also need to capture it, so we use a cgroup to captue it:
/YM#([0-9]+-[0-9]+)/g
Then we need to refer to the capture group again, so we use the following code instead of the String.match
var regex = /YM#([0-9]+-[0-9]+)/g;
var match = regex.exec(text);
var id = match[1];
// 0: match of entire regex
// after that, each of the groups gets a number
(?!<li>YM#\s)([\d-]+)
http://regexr.com?30ng5
This will match the numbers.
Try this:
(<li>[^#<>]*?# *)([\d\-]+)\b
and get the result in $2.
I need to capture the price out of the following string:
Price: 30.
I need the 30 here, so I figured I'd use the following regex:
([0-9]+)$
This works in Rubular, but it returns null when I try it in my javascript.
console.log(values[1]);
// Price: 100
var price = values[1].match('/([0-9]+)$/g');
// null
Any ideas? Thanks in advance
Try this:
var price = values[1].match(/([0-9]+)$/g);
JavaScript supports RegExp literals, you don't need quotes and delimiters.
.match(/\d+$/) should behave the same, by the way.
See also: MDN - Creating a Regular Expression
Keep in mind there are simpler ways of getting this data. For example:
var tokens = values[1].split(': ');
var price = tokens[1];
You can also split by a single space, and probably want to add some validation.
Why don't you use this?
var matches = a.match(/\d+/);
then you can consume the first element (or last)
my suggestion is to avoid using $ in the end because there might be a space in the end.
This also works:
var price = values[1].match('([0-9]+)$');
It appears that you escaped the open-perens and therefore the regex is looking for "(90".
You don't need to put quotes around the regular expression in JavaScript.