splitting a string based on delimiter [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I split a string, breaking at a particular character?
I have a string in following format
part1/part2
/ is the delimiter
now I want to get split the string and get part 1. How can I do it?

result = "part1/part2".split('/')
result[0] = "part1"
result[1] = "part2

split the string and get part 1
'part1/part2'.split('/')[0]

var tokens = 'part1/part2'.split('/');

var delimeter = '/';
var string = 'part1/part2';
var splitted = string.split(delimeter);
alert(splitted[0]); //alert the part1

var result = YourString.split('/');
For your example result will be an array with 2 entries: "part1" and "part2"

Related

How to trim string between given parameter from existing string in javascript [duplicate]

This question already has answers here:
How to remove the end of a string, starting from a given pattern?
(5 answers)
Closed last month.
I have the string "/Employee/Details/568356357938479"; and I want to obtain the new string of only "/Employee" from the given string?
var myString = "/Employee/Details/568356357938479";
var newString = myString.replace(/\\|\//g, '');
I'm expecting : "/Employee"
try this:
var myString = "/Employee/Details/568356357938479";
var newString = myString.substring(0, myString.indexOf("/", 1));
console.log(newString);

Remove string quotes from a array javascript? [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Convert string into an array of arrays in javascript
(3 answers)
Closed 2 years ago.
I'm trying to remove " " from an array inside a string.
var test = "['a']"
var test1 = "['a','b']"
Expected Output:
var test_arr = ['a']
var test1_arr = ['a','b']
I tried replacing, didn't work
var test_arr = test.replace(/\"/, '');
I see two ways to accomplish that.
JSON.parse('["a","b"]') note that the values need to be in double-quotes.
"['a','b']".replace(/[['\]]/g, '').split(',') note that you need to split after replacing the unwanted chars
Both yield an array containing the original strings.
You can simply convert the single quotes inside the strings to double quotes first to convert the string to a valid JSON, and then we can use JSON.parse to get the required array like:
var test = "['a']"
var test1 = "['a','b']"
var parseStr = str => JSON.parse(str.replace(/'/g, '"'))
var test_arr = parseStr(test)
var test1_arr = parseStr(test1)
console.log(test_arr)
console.log(test1_arr)

Split text with {{Text}} format Javascript [duplicate]

This question already has answers here:
Regex to get string between curly braces
(16 answers)
Closed 4 years ago.
Sorry to bother you all. I'm no idea about regular expression. But right now I need one very badly.
I want to split text using this format {{Text}}. The "Text" can be anything. All I need is split the text at the position of {{Text}}.
Here is a sample.
var Regx = My Regx;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = String.split(Regx);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]
Thank you in advance.
Edit:
I solved it myself too. It is {{[^{}]+}}
You can do this way
var test = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = test.split(/\{\{.*?\}\}/);
console.log(SplitArray)
Try this:
var Regx = /\{\{.*?\}\}/;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = String.split(Regx);
console.log(SplitArray);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]

Javascript get the string between two symbols [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have the following string and I'm trying to retrieve the string between two symbols
http://mytestdomain.com/temp-param-page-2/?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=720960&wpv_post_search&wpv-women-clothing[]=coats
I need to retrieve wpv-women-clothing[] or any other string between the last & and the last = in the URL
Should I use regex for this or is there a function in Javascript/jQuery already well suited for this?
Thanks
var str = "http://mytestdomain.com/temp-param-page-2/?wpv_paged_preload_reach=1&wpv_view_count=1&wpv_post_id=720960&wpv_post_search&wpv-women-clothing[]=coats";
var last =str.split('&').pop().split('=')
console.log(last[0]) // wpv-women-clothing[]
jsFiddle example
Split the string on the ampersands (.split('&')), take the last one (.pop()), then split again on the = (.split('=')) and use the first result last[0].
.*&(.*?)=.*
This should do it.
See demo.
http://regex101.com/r/lZ5bT3/1
Group index 1 contains your desired output,
\&([^=]*)(?==[^&=]*$)
DEMO
> var re = /\&([^=]*)(?==[^&=]*$)/g;
undefined
> while ((m = re.exec(str)) != null) {
... console.log(m[1]);
... }
wpv_post_search&wpv-women-clothing[]
Can you try:
var String = "some text";
String = $("<div />").html(String).text();
$("#TheDiv").append(String);

How to trim a string to its last four characters? [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I know there are methods to remove characters from the beginning and from the end of a string in Javascript. What I need is trim a string in such a way that only the last 4 characters remain.
For eg:
ELEPHANT -> HANT
1234567 -> 4567
String.prototype.slice will work
var str = "ELEPHANT";
console.log(str.slice(-4));
//=> HANT
For, numbers, you will have to convert to strings first
var str = (1234567).toString();
console.log(str.slice(-4));
//=> 4567
FYI .slice returns a new string, so if you want to update the value of str, you would have to
str = str.slice(-4);
Use substr method of javascript:
var str="Elephant";
var n=str.substr(-4);
alert(n);
You can use slice to do this
string.slice(start,end)
lets assume you use jQuery + javascript as well.
Lets have a label in the HTML page with id="lblTest".
<script type="text/javascript">
function myFunc() {
val lblTest = $("[id*=lblTest]");
if (lblTest) {
var str = lblTest.text();
// this is your needed functionality
alert(str.substring(str.length-4, str.length));
} else {
alert('does not exist');
}
}
</script>
Edit: so the core part is -
var str = "myString";
var output = str.substring(str.length-4, str.length);

Categories