Substring a part of a string using angularjs - javascript

I have a string like this string(1), I want to get substring remove the last part to obtain just string any suggestions please!!
PS.the string could be: sringggg(125).

You can use various options to get the desired string.
//With regular expression with split() and fetch the first element of array
console.log('sringggg(125)'.split(/\(\d+\)/)[0]);
//Using string with split() and fetch the first element of array
console.log('sringggg(125)'.split('(')[0]);
//Using substr and indexOf
var str = 'sringggg(125)'
console.log(str.substr(0, str.indexOf('(')));
References
String.prototype.split()
String.prototype.indexOf()

If string is always in same pattern and ' ( ' is a separator use split .
var string = 'string(1)'
var result = string .split('(')[0];

Try using regexp
var tesst = "string(1)"
var test = tesst.match(/^[^\(]+/);
// test = "string"

Related

Split a single array with respect to comma in javascript

I am getting an array of single string $scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"];
The split should be by watching ,
I need to break it down = ["Talking Spanish","Spanish food","Spanish things","Football"];
How can I do it using javascript?
You can use split
let arr = ["Talking Spanish,Spanish food,Spanish things,Football"];
let op = arr[0].split(',')
console.log(op)
You can split 0th index of $scope.Obj with , and reassign to $scope.Obj.
$scope={};
$scope.Obj= ["Talking Spanish,Spanish food,Spanish things,Football"];
$scope.Obj=$scope.Obj[0].split(",")
console.log($scope.Obj);
str.split(separator, limit)
The above is common syntax to split a array
https://medium.freecodecamp.org/how-to-make-your-react-native-app-respond-gracefully-when-the-keyboard-pops-up-7442c1535580
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
In the following example, split() looks for spaces in a string and returns the first 3 splits that it finds.
var myString = 'Hello World. How are you doing?';
var splits = myString.split(' ', 3);
console.log(splits);
This script displays the following:
["Hello", "World.", "How"]

Splitting String and taking out specific elements in node js

I have Sample String like this
"Organisation/Guest/images/guestImage.jpg"
I need to take out Organisation,Guest separately.
I have tried split() but can't get desired output.
var str = "Organisation/Guest/images/guestImage.jpg";
var res = str.split("/");
console.log(res[0]);
console.log(res[1]);
You can use of String.replace() along with regex
const regex = /Organisation\/|\/Organisation/;
console.log('Organisation/Guest/images/guestImage.jpg'.replace(regex, ''));
console.log('Guest/Organisation/images/guestImage.jpg'.replace(regex, ''));
console.log('Guest/images/guestImage.jpg/Organisation'.replace(regex, ''));
var yourString = "Organisation/Guest/images/guestImage.jpg";
yourString.split('/')
// this returns all the words in an array
yourString[0] // returns Organisation
yourString[1] // returns Guest and so on
When you run .split() on a string, it will return a new array with all the words in it. In the code I am splitting by the slash /
Then I save the new array in a variable. Now you should know we can access array properties like this: array[0] where 0 is the first index position or the first word, and so on.

Regex or lastIndexOf when removing last instance of a string

I couldn't apply answers to other similar questions (trying to replace the last occurrence of a string) because I am having trouble with the syntax.
I'm trying to replace the last occurrence of a string. The value of the string is stored in a variable that is passed to the .replace() method like this:
var str += some additive strings;
var del = a string that lives within str; // the value is dynamic
str = str.replace(del$, ''); // this doesn't work to remove the last occurrence of `del` in str
As I understand it the $ argument looks for the last occurrence of a string within a regex; but I can't figure out how to use it alongside a variable passed to .replace(). Any suggestions?
If you want to use the RegExp with a $, do it like this:
var str += 'some additive strings';
var re = new RegExp('a string that lives within str$');
str = str.replace(re, '');
str = str.substring(0, str.length - del.length);
You can use lastIndexOf() with slice()
Example:
var str ="HI this is cool isn't it? cool";
var del='cool';// put whatever here
var index = test.lastIndexOf(del);
var length=del.length;
if(index!=-1)
var removeStr=test.substr(index,length);
str.replace(removeStr,''); // HI this is cool isn't it?
Mate, this is just an answer. You'll need to use it according to your needs.
Updated Live demo:http://jsfiddle.net/n2Kgn/2

Return only text after last underscore in JavaScript string

If I have a string like so:
var str = 'Arthropoda_Arachnida_Zodariidae_Habronestes_hunti';
How can I get just the last part of the string after the last underscore?
And in the case there are no underscores just return the original string.
In this case I want just 'hunti'
var index = str.lastIndexOf("_");
var result = str.substr(index+1);
It's very simple. Split the string by the underscore, and take the last element.
var last = str.split("_").pop();
This will even work when the string does not contain any underscores (it returns the original string, as desired).
You can use a regular expression:
'Arthropoda_Arachnida_Zodariidae_Habronestes_hunti'.match(/[^_]*$/)[0];

Parse string in javascript

How can I parse this string on a javascript,
var string = "http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1";
I just want to get the "265956512115091" on the string. I somehow parse this string but, still not enough to get what I wanted.
my code:
var newstring = string.match(/set=[^ ]+/)[0];
returns:
a.265956512115091.68575.100001022542275&type=1
try this :
var g=string.match(/set=[a-z]\.([^.]+)/);
g[1] will have the value
http://jsbin.com/anuhog/edit#source
You could use split() to modify your code like this:
var newstring = string.match(/set=[^ ]+/)[0].split(".")[1];
For a more generic approach to parsing query strings see:
Parse query string in JavaScript
Using the example illustrated there, you would do the following:
var newstring = getQueryVariable("set").split(".")[1];
You can use capturing group in the regex.
const str = 'http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1';
console.log(/&set=(.*)&/.exec(str)[1]);

Categories

Resources