How to trim string between slash? - javascript

I need to take vale between / slashes. for example ./ankits/ankitt$. Here I need to take the ‘antits’ string. Do i need to use reg-expression or it can be done using trim method?
Please help me to solve this

What about .split method?
var str = './ankits/ankitt$.';
var arr = str.split('/');
console.log(arr);
It will split the string in array, forward slash will be used as separator.

This regular expression will return what's inbetween the slashes:
/\/(.*?)\//
Test code:
var regex = /\/(.*?)\//;
var str = './ankits/ankitt$.';
var result;
result = regex.exec(str);
alert(result[0]);
Short form:
"./ankits/ankitt$".match(/\/(.*?)\//)[1]

Try this
"./ankits/ankitt$".match(/\/([^\/]+)\//)[1]

Related

Regex - convert the string to camel caps by `dot`

I am looking for a solution to convert my string to camelcaps by the dot it contains.
here is my string: 'sender.state'
I am expecting the result as : 'senderState';
I tried this: 'sender.state'.replace(/\./g, ''); it removes the . but how to handle the camel caps stuff?
You can pass a function to .replace():
'sender.state'.replace(/\.([a-z])/g, (match, letter) => letter.toUpperCase());
Without regex - this might help :
var str = "bad.unicron";
var temp = str.split(".");
return temp[0]+temp[1].charAt(0).toUpperCase() + temp[1].slice(1);
I'll try to come up with regex

How Can I Use Regex To Split A String on Letters and Numbers and Carets followed by digits

So I was wondering how I would go about splitting a String on specific characters/conditions using Regular Expressions like the following:
On digits
On Letters
On digits following a caret
Here could be an example:
var str1 = "62y^2";
Would return as an array:
[62,y,^2]
Thanks for the help.
You can use String.match() instead of split with the following regular expression (Regex101):
var str="62y^2ad23^123";
var result = str.match(/\^?\d+|[a-z]+/gi);
console.log(result);
You may try the following approach:
var str="62y^2ad23^123";
console.log(str.split(/(\^\d+|[a-zA-Z]+|\d+)/).filter(function(n){ return n != "" }));
Try the below regex
var str = "62y^2";
str.match(/\^(\d+|[a-zA-Z]+)|[a-zA-Z]+|\d+/g); // ["62", "y", "^2"]

How to remove strings with numbers and special characters using regular expression

Remove strings with numbers and special characters using regular expression.Here is my code
var string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
var numbers = string.match(/(\d+)/gi);
alert(numbers.join(','));
here output is : 0,1,1,2,2,3,3,4
But i want the following output 1,2,3,4
Can any one please help me.
Thanks,
Seemd what you want is [\d+], use exec like this,
var myRe = /\[(\d+)\]/gi;
var myArray, numbers = [];
while ((myArray = myRe.exec(string)) !== null) {
numbers.push(myArray[1]);
}
http://jsfiddle.net/xE265/
You can do:
string = "[Account0].&[1]+[Account1].&[2]+[Account2].&[3]+[Account3].&[4]";
repl = string.replace(/.*?\[(\d+)\][^\[]*/g, function($0, $1) { return $1 });
//=> "1234"
I guess the simplest solution in this case would be:
\[(\d+)\]
simply saying that you only want the digits enclosed by brackets.
Regards

Finding REGEX for this Expression(javascript)

I have the following string in java script
href="http://site.com/colours/254359457969591" title="hello"
I need to get the value 254359457969591 from the above href string.I tried with many methods. Can anybody guide me to solve this problem?
Well, just /\d+/ would work in this example.
var s = "href=\"http://site.com/colours/254359457969591\" title=\"hello\"";
var result = /href="http:\/\/site.com\/colours\/(\d+)"/.exec(s);
var num = result[1];
The result of num is: 254359457969591
Here's how you would get it with javascript's Match function:
str = 'href="http://site.com/colours/254359457969591" title="hello"';
patt1 = /\d+/;
document.write(str.match(patt1));
UPDATE
If you want to get the numbers between colours/ and " then use this regex:
/colours\/(\d+)"/
Here's the match function updated:
str = 'href="http://site.com/colours/254359457969591" title="hello"';
patt1 = /colours\/(\d+)"/;
document.write(str.match(patt1)[1]);
m/href="http:\/\/site\.com/colours/([0-9]+)/i
$1 is the number.
Just use the javascript provided regex functions and that's it.

how to extract string part and ignore number in jquery?

I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");

Categories

Resources