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 + ''));
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 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)
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.
I have a working reg expression that does a replace function based on the expression. It works perfect. It finds a specific string based on the beginning of the string and the expression. This is it:
str.replace(/\bevent[0-9]*\=/, "event");
what this does is it changes event=1 to event.
What if event was a variable word? What if I needed to look for conference also?
I have tried:
var type = "conference";
str.replace(/\b/ + type+ /[0-9]*\=/, "conference");
and:
str.replace(/\b/type/[0-9]*\=/, "conference");
neither worked.
how can I pass a javascript string into a regular expression?
Instead of writing a RegEx literal, use a string to create a new RegExp object:
str.replace(new RegExp('\b' + var + '[0-9]'), …)
You can do that with the RegExp Object:
str.replace(RegExp('\b' + reStr + '[0-9]*\='),StrToReplaceWith)
Create a new regex object with your variable.
read this...
http://www.w3schools.com/js/js_obj_regexp.asp
I am using a regular expression validation to validate my form using javascript.
my pattern is
/^[a-zA-Z]+[a-zA-Z0-9_]*\\.{0.1}[a-zA-Z0-9_]*/
is this pattern correct for validating "not starting from number","only 1 '.' allowed and then text"?
wat can i use in javascript for validation?? something like preg_match func of php.
I m not familiar with regular expressions so help me and give sum func to do this for me.
i tried to use regexp object but cant get result from .exec,test function.
Yes, the regexp looks good. Require 1 char, followed by 0..inf char, number or _, followed by an optional dot, followed by 0..inf char, number or _.
You have one syntacticaly error though. Replace the {0.1} with {0,1}.
You can then use the .test() function and pass it a string to test.
var a = "bb";
var r = /^[a-zA-Z][a-zA-Z0-9_]*\.{0,1}[a-zA-Z0-9_]*/;
r.test(a);
returns true.