I have a string that look like this :
blablablablafunction tr(b){b=b.split("");b=b.reverse();b=b.slice(2);return b.join("")}blablablabla
And i want to get : b=b.split("");b=b.reverse();b=b.slice(2);return b.join("")
with Regex :
var match = "function tr(b){(.*)}";
var f = html.match(match);
And i get null in f.Any idea what is the problem?
You will have to escape special characters in the regex in this case I believe these are { , } and also ( and )(around the function argument list). Use the escape character(\) to do that. So try this regex:
var match = "function tr\\(b\\)\\{(.*)\\}";
Related
I have a variable which contain a string and I want to return only the letters from regular expression (“b” and “D”) or any letter that I indicate on regular expression from match().
var kk = "AaBbCcDd".match(/b|D/g);
kk.forEach(function(value,index){
console.log(value,index)
});
My problem is that regular expression I think because is returning b and D but the index is not the index from kk variable and I'm not really sure, why ... so if someone can help me a little bit because I stuck
The match method from javascript only returns an array with the given match:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match
You would need to implement a new function which will loop through all characters of your string and return the given index of the matches.
This method could use the function search from String.prototype: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/search
You have to write a new function to get the index of the matched regex like a sample below:-
var re = /bar/g,
str = "foobarfoobar";
while ((match = re.exec(str)) != null) {
alert("match found at " + match.index);
}
Hope this will help you
Actually this is the answer :
var kk = "AaBbCcDd".match(/B?d?/g);
kk.forEach(function(value,index){
console.log(value,index)
});
if someone will encounter this scenario ...
The match() regular expresion B?d? will return an array indicating the position of "B" and "d" of the initial array kk.
Example of text:
Some string here : my value
Another string : my value
String : my value
I want to match everything before and including the symbol :
My wanted output is:
Some string here :
Another string :
String :
Thanks
Just use:
(.* :)
See example: https://regex101.com/r/bA1cQ1/2
Don't use a regular expression, because it's not a nail to regex's hammer.
var strToMatch = "Some string here : my value";
var match = strToMatch.slice(0,strToMatch.indexOf(':')+1);
// do something with the match
document.body.appendChild(document.createElement('pre')).innerHTML = match;
I have this String :
var test = "toto#test.com";
I would like to replace all character after the "#" character by empty value.
I would like to get this String :
var test = "toto"
Try this:
test= test.split('#')[0]
"toto#test.com".replace(/#.+$/, '')
Is it possible to check whether a string contains only special characters using javascript?
Match where the string has only characters, that are not in the class defined here as a-z, A-Z or 0-9.
var regex = /^[^a-zA-Z0-9]+$/
Test it...
console.log( "My string to test".match(regex) )
console.log( "My string, to test!".match(regex) )
Yes it is possible. Try below, it should work.
function hasOnlySpecialCharater(val) {
var pattern = /^[^a-zA-Z0-9]+$/;
return (pattern.test(val));
}
console.log(hasOnlySpecialCharater("##$"));
console.log(hasOnlySpecialCharater("ChiragP&^"));
We can write this :
function checkOnlySplChar(str) {
// to check if string contains only special characters
var alphaNumOnly = RegExp('/^[^a-zA-Z0-9]+$/');
return (alphaNumOnly.test(str));
}
I wrote a regular expression which I expect should work but it doesn't.
var regex = new RegExp('(?<=\[)[0-9]+(?=\])')
JavaScript is giving me the error:
Invalid regular expression :(/(?<=[)[0-9]+(?=])/): Invalid group
Does JavaScript not support lookahead or lookbehind?
This should work:
var regex = /\[[0-9]+\]/;
edit: with a grouping operator to target just the number:
var regex = /\[([0-9]+)\]/;
With this expression, you could do something like this:
var matches = someStringVar.match(regex);
if (null != matches) {
var num = matches[1];
}
Lookahead is supported, but not lookbehind. You can get close, with a bit of trickery.
To increment multiple numbers in the form of lets say:
var str = '/a/b/[123]/c/[4567]/[2]/69';
Try:
str.replace(/\[(\d+)\]/g, function(m, p1){
return '['+(p1*1+1)+']' }
)
//Gives you => '/a/b/[124]/c/[4568]/[3]/69'
If you're quoting a RegExp, watch out for double escaping your backslashes.