I can't replace the substring in a string:
var source = "div.col-md-4.fields:visible:eq(0) div.panel-body select:eq(0)";
var modified = source.replace(/visible:eq(0)/g, "1234");
I wonder why does modified have the same value as source?
You should not use regular expressions here but a simple string replace function. It will run faster and regular expressions were not made for simple tasks like this as they will run slightly slower than the simple replace function. Using regular expressions here is like using a nuke to open a water bottle, rather prefer simplicity, if a developer sees this code he will like the simplicity.
Change your second line to this one:
var modified = source.replace("visible:eq(0)", "1234");
You need to escape the brackets
var source = "div.col-md-4.fields:visible:eq(0) div.panel-body select:eq(0)";
var modified = source.replace(/visible:eq\(0\)/g, "1234");
console.log(source);
console.log(modified);
You just need to escape your chars like this Demo: http://jsfiddle.net/cvW24/1/
hope rest help the cause :)
if you keen:
Need to escape a special character in a jQuery selector string
http://api.jquery.com/category/selectors/
code
var source = "div.col-md-4.fields:visible:eq(0) div.panel-body select:eq(0)";
var modified = source.replace(/visible:eq\(0\)/g, "1234");
alert(modified);
Because your regular expression does not match the string. You need to escape the parenthesis.
var modified = source.replace(/visible:eq\(0\)/g, "1234");
Related
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 have several Javascript strings (using jQuery). All of them follow the same pattern, starting with 'ajax-', and ending with a name. For instance 'ajax-first', 'ajax-last', 'ajax-email', etc.
How can I make a regex to only grab the string after 'ajax-'?
So instead of 'ajax-email', I want just 'email'.
You don't need RegEx for this. If your prefix is always "ajax-" then you just can do this:
var name = string.substring(5);
Given a comment you made on another user's post, try the following:
var $li = jQuery(this).parents('li').get(0);
var ajaxName = $li.className.match(/(?:^|\s)ajax-(.*?)(?:$|\s)/)[1];
Demo can be found here
Below kept for reference only
var ajaxName = 'ajax-first'.match(/(\w+)$/)[0];
alert(ajaxName);
Use the \w (word) pattern and bind it to the end of the string. This will force a grab of everything past the last hyphen (assuming the value consists of only [upper/lower]case letters, numbers or an underscore).
The non-regex approach could also use the String.split method, coupled with Array.pop.
var parts = 'ajax-first'.split('-');
var ajaxName = parts.pop();
alert(ajaxName);
you can try to replace ajax- with ""
I like the split method #Brad Christie mentions, but I would just do
function getLastPart(str,delimiter) {
return str.split(delimiter)[1];
}
This works if you will always have only two-part strings separated by a hyphen. If you wanted to generalize it for any particular piece of a multiple-hyphenated string, you would need to write a more involved function that included an index, but then you'd have to check for out of bounds errors, etc.
How do I manipulate a string using MooTools / JavaScript
I would like to replace all after /p/ in following url:
http://example.com/groups/browse/catId/14/p/1000-1500
Expected result:
http://example.com/groups/browse/catId/14
That doesn't look like you want to replace, it looks like you want to remove.
You can do that with regular string operations in plain Javascript:
var s = "http://example.com/groups/browse/catId/14/p/1000-1500";
s = s.substr(0, s.indexOf("/p/")));
Simple enough with regex:
var url = "http://example.com/groups/browse/catId/14/p/1000-1500";
console.log(url.replace(/\/p\/.+/, ""));
//-> "http://example.com/groups/browse/catId/14"
In the regex above, \/p\/ is /p/ with escaped slashes, followed by .+ which means match any character (except white space) one or more times.
You can brush up on your JavaScript regular expressions at http://www.regular-expressions.info/javascript.html.
you don't have to use mootools necessarily. you can split the string with split function from javascript
var myString = 'http://example.com/groups/browse/catId/14/p/1000-1500';
newString = myString.split('/p/');
alert(newString[0]);
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.