i want to extract 34 from this string. How Can i done that ? (i will use javascript)
#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30
var src = "#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30",
match = /project_subcategory=(\d+)/g.exec(src);
alert(match[1]);
Anyways, it looks like a query string so there should be a better way to parse/read that string. See http://blog.falafel.com/Blogs/AdamAnderson/07-12-17/Parse_a_Query_String_in_JavaScript.aspx
.*project_subcategory=(\d*).*
Related
Let's say I have this String : str = '236112456'
I want to change '236' with something else, and if a character is alone, change by another thing.
i tried something like that :
var result =
str.replaceAll('236', '236.jpg')
.replaceAll('1', '1.jpg')
.replaceAll('2', '2.jpg')
.replaceAll...
but it won't take 236 and only change individual letters...
How can I achieve that ?
Thanks in advance for your help :)
[edit] - Mistakes in the code example
Here is the solution.
Please use regex expression instead of '23' string.
str = '236112456';
var result = str.replaceAll(/236/gi, '236.jpg').replaceAll(/1/gi, '1.jpg');
...
The result is "236.jpg1.jpg1.jpg2456". Is this not what you are looking for ?
I've got a string 'url(data:image/png;base64,iVBORw0K...GgoA)'.
I need to invoke only base64 data from it. In output i'd like to see something like this ['iVBORw0K...GgoA'].
Could anyone help me with creating a correct RegExp expression?
Thanks in advance.
.*base64,(\w+)\)$
If you get group 1 from the regex, you will get the base64 data you want.
You can write a regular expression like this,
var phrase = "url(data:image/png;base64,iVBORw0K...GgoA)";
var myRegexp = /base64,(.*)/;
var match = myRegexp.exec(phrase);
alert(match[1]);
HTH
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 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.
Let's say I have something like this:
var location = '/users/45/messages/current/20/';
and I need to end up with this:
'/45/messages/current/20/'
So, I need to erase the first part of /whatever/
I can use jquery and/or javascript. How would I do it in the best way possible?
To replace everything up to the second slash:
var i = location.indexOf("/", 1); //Start at 2nd character to skip first slash
var result = location.substr(i);
You can use regular expressions, or the possibly more readable
var location = '/users/45/messages/current/20/';
var delim = "/"
alert(delim+location.split(delim).slice(2).join(delim))
Use JavaScript's slice() command. Do you need to parse for where to slice from or is it a known prefix? Depending on what exactly you are parsing for, it may be as simple as use match() to find your pattern.
location.replace("/(whatever|you|want|users|something)/", "");
find the 2nd index of '/' in the string, then substr from the index to the end.
If you always want to eliminate the first part of a relative path, it's probably simplest just to use regular expressions
var loc = '/users/45/messages/current/20/';
loc.replace(/^\/[^\/]+/,'');
location.replace(/\/.*?\//, "/");
location.replace(/^\/[^\/]+/, '')