You may try a regex string split here:
var inputs = ["£____", "____$", "42____$"];
for (var i=0; i < inputs.length; ++i) {
var parts = inputs[i].split(/_+/);
console.log("before = " + parts[0] + ", after = " + parts[1]);
}
Answer from Tim Biegeleisen on Stack OverflowYou may try a regex string split here:
var inputs = ["£____", "____$", "42____$"];
for (var i=0; i < inputs.length; ++i) {
var parts = inputs[i].split(/_+/);
console.log("before = " + parts[0] + ", after = " + parts[1]);
}
You can try Regex.
Something Like This
[^_;]
const streetAddress = addy.substring(0, addy.indexOf(","));
While it’s not the best place for definitive information on what each method does (MDN Web Docs are better for that) W3Schools.com is good for introducing you to syntax.
var streetaddress = addy.split(',')[0];
How do I have JavaScript get a substring before a character? - Stack Overflow
javascript - How to return part of string before a certain character? - Stack Overflow
jquery - javascript get string before a character - Stack Overflow
javascript - Getting values before and after characters "-" in a String - Stack Overflow
Do you mean substring instead of subscript? If so. Then yes.
var string = "55+5"; // Just a variable for your input.
function getBeforePlus(str){
return str.substring(0, str.indexOf("+"));
/* This gets a substring from the beginning of the string
to the first index of the character "+".
*/
}
Otherwise, I recommend using the String.split() method.
You can use that like so.
var string = "55+5"; // Just a variable for your input.
function getBeforePlus(str){
return str.split("+")[0];
/* This splits the string into an array using the "+"
character as a delimiter.
Then it gets the first element of the split string.
*/
}
Yes. Try the String.split method: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/split
split() returns an array of strings, split by the character you pass to it (in your case, the plus). Just use the first element of the array; it will have everything before the plus:
const string = "foo-bar-baz"
const splittedString = string.split('-')
//splittedString is a 3 element array with the elements 'foo', 'bar', and 'baz'
You fiddle already does the job ... maybe you try to get the string before the double colon? (you really should edit your question) Then the code would go like this:
str.substring(0, str.indexOf(":"));
Where 'str' represents the variable with your string inside.
Click here for JSFiddle Example
Javascript
var input_string = document.getElementById('my-input').innerText;
var output_element = document.getElementById('my-output');
var left_text = input_string.substring(0, input_string.indexOf(":"));
output_element.innerText = left_text;
Html
<p>
<h5>Input:</h5>
<strong id="my-input">Left Text:Right Text</strong>
<h5>Output:</h5>
<strong id="my-output">XXX</strong>
</p>
CSS
body { font-family: Calibri, sans-serif; color:#555; }
h5 { margin-bottom: 0.8em; }
strong {
width:90%;
padding: 0.5em 1em;
background-color: cyan;
}
#my-output { background-color: gold; }
Another method could be to split the string by ":" and then pop off the end.
var newString = string.split(":").pop();
This is the what you should use to split:
string.slice(0, string.indexOf("'"));
And then to handle your non existant value edge case:
function split(str) {
var i = str.indexOf("'");
if(i > 0)
return str.slice(0, i);
else
return "";
}
Demo on JsFiddle
Nobody seems to have presented what seems to me as the safest and most obvious option that covers each of the cases the OP asked about so I thought I'd offer this:
function getCharsBefore(str, chr) {
var index = str.indexOf(chr);
if (index != -1) {
return(str.substring(0, index));
}
return("");
}
You can do this in one line using destructuring
A working example
var [before, after] = 'Username|Jackie'.split("|")
console.log(before)
console.log(after)
The above is the same as doing this:
var pieces = 'Username|Jackie'.split("|")
var before = pieces[0]
var after = pieces[1]
console.log(before)
console.log(after)
.split() will take a string and generate an array around the delimiter you define. (String.prototype.split()) This means that running .split() on your strings will return an array of length 2 with your value in index 1.
const username = "Username|Jackie";
const arr = username.split("|");
console.log(arr); // ["Username", "Jackie"];
You can access these values using index based array manipulation or the array destructuring notation. (Array Destructuring)
const username = "Username|Jackie";
const arr = username.split("|");
console.log(arr); // ["Username", "Jackie"];
console.log(arr[0]); // "Username"
console.log(arr[1]); // "Jackie"
const [type, val] = username.split("|");
console.log(type); // "Username"
console.log(val); // "Jackie"
Split separates the string into tokens separated by the delimiter. It always returns an array one longer than the number of tokens in the string. If there is one delimiter, there are two tokens—one to the left and one to the right. In your case, the token to the left is the empty string, so split() returns the array ["", "mysite.com/username_here80"]. Try using
var urlsplit = url.split("mycool://?string=")[1]; // <= Note the [1]!
to retrieve the second string in the array (which is what you are interested in).
The reason you are getting a comma is that converting an array to a string (which is what alert() does) results in a comma-separated list of the array elements converted to strings.
The split function of the string object returns an Array of elements, based on the splitter. In your case - the returned 2 elements:
var url = "http://DOMAIN.com/username_here801";
var urlsplit = url.split("//");
console.log(urlsplit);
The comma you see is only the representation of the Array as string.
If you are looking for to get everything after a substring you better use the indexOf and slice:
var url = "http://DOMAIN.com/username_here801";
var splitter = '//'
var indexOf = url.indexOf(splitter);
console.log(url.slice(indexOf+splitter.length));