for example if we consider document.createElement() function the parameter can be passed in 3 ways
var v="script";
var s=document.createElement(v);
var s=document.createElement("script");
var s=document.createElement('scipt');
i want a regular expression which extracts the parameter in document.createElement function excluding quotes. I tried this by using groups but i am writing two regular expression one for "",'' and other for normal variable
please provide an example
var re = /document\.createElement\((['"]*)(.+?)\1\)/;
The result is in:
str.match(re)[2];
http://jsfiddle.net/mihaifm/RWc8N/
Use pipe - | for OR operations.
For example: script|scipt.
Related
With c# there is a string.Replace-method.
Like This:
string oldString = "stackoverflow";
string newString= oldString.Replace("stackover","");
Output: flow
Can I do something similar to this with AngularJs?
My try doesn't work:
var oldString = "stackoverflow";
$scope.newString= oldString.Replace("stackover","NO");
In Javascript method names are camel case, so it's replace, not Replace:
$scope.newString = oldString.replace("stackover","NO");
Note that contrary to how the .NET Replace method works, the Javascript replace method replaces only the first occurrence if you are using a string as first parameter. If you want to replace all occurrences you need to use a regular expression so that you can specify the global (g) flag:
$scope.newString = oldString.replace(/stackover/g,"NO");
See this example.
The easiest way is:
var oldstr="Angular isn't easy";
var newstr=oldstr.toString().replace("isn't","is");
var oldString = "stackoverflow";
var str=oldString.replace(/stackover/g,"NO");
$scope.newString= str;
It works for me.
Use an intermediate variable.
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");
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 using jasmine for testing JavaScript code.
I would like to check the content of render function in this way:
expect(this.view.el.innerHTML).toContain(''+ 'regexp(any text)' +'');
would be possible to pass some parameter as a regular expression?
If yes, how?
I think you would need to use the toMatch matcher which takes a regular expression (toContain expects a string parameter) and build your regular expression by concatenating the fixed and variable strings something like this:
var searchString = ...
expect(innerHTML).toMatch(new RegExp('' + searchString + ''));
If I had a string with three values that is delimited by spaces, now I want to store these three values in three variables or maybe an array, how to do?
Use split().
For example:
var variables = delimited_string.split(/\s+/);
If you know it is separated by a single space, avoid using a regular expression with the following:
var variables = delimited_string.split(' ');
Got it: I use String.split(pattern)
var str = "I am confused".split(/\s/g)