Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Is there a function in Java Script that divide a string with several arguments?
var string = "This is a test text, again, this is a testing text";
For example, I can split with , by saying string.split(','); and I will have:
var string = ["This is a test text", "again", "this is a testing text"];
Now, I want to split it with several parameters, so string now would be
var string = ["test text", "testing text"]
I'm looking for a function that extract all the parts that start with test and end with text.
I'm not sure that I understood what you want, but here is a function I wrote in 2 minutes. With the following scenario
var string = "This is a test text, again, this is a testing text";
function customSplit(str_to_split, start_string, end_string) {
var res = [];
var start_index, end_index;
for (i = 0; i <= str_to_split.length; i++) {
start_index = str_to_split.toLowerCase().indexOf(start_string.toLowerCase(), i);
if (i == start_index) {
end_index = str_to_split.toLowerCase().indexOf(end_string.toLowerCase(), i);
if (end_index >= 0) {
res.push(str_to_split.substring(start_index, end_index + end_string.length));
}
}
}
return res;
}
console.log(customSplit(string, "test", "text"));
it will output ["test text", "testing text"].
Let me know if it helps you.
EDIT
Corrected a wrong behave with a particular string. Please remind that I wrote it in a couple of minutes.
Use a regex:
var str = "This is a test text, again, this is a testing text";
console.log(str.match(/test.+?text/g));
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I am creating a "Silly Story Generator" in Javascript and after fixing a few errors that popped up I encountered "SyntaxError: missing ) after argument list"
After reading more about it I learned that it occurs when there is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string.
I checked my code and I cannot seem to find the mistake, string on line 38 looks okay.
Thank you.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
Your Code is Badly formatted.
At newStory.contentString.replace("insertX", "insertY", "insertZ";)
You had a semi-colon inside the the parenthesis.
You are also missing two curly braces near the end.
I suggest getting a good IDE or using the formatting features that come with the one you use.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
you have written a semicolon before the closing parentheses
newStory.contentString.replace("insertX", "insertY", "insertZ");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string like A/B/C/D.txt
So I want to replace the character just before the last . with the new one.
Like I would add E in last So the output should be A/B/C/E.txt
Can this is possible with only replace and concat Or Regex should help?
I'm sure the output is correct.
A/B/C/E.txt
var filename = 'A/B/C/D.txt';
var dotIndex = filename.lastIndexOf('.');
document.write(filename.substr(0, dotIndex-1) + 'E'+filename.substr(dotIndex, filename.length-dotIndex));
var parts = 'A/B/C/D.txt'.split('.');
if(parts.length > 1)
{
parts[parts.length-2] = parts[parts.length-2].replace(/.$/,"E");
let result = parts.join('.');
console.log(result);
}
Try this:
let name = "A/B/C/D.txt"
let res = name.substr(0, name.lastIndexOf(".")) + "E." + name.substr(name.lastIndexOf(".") + 1);
console.log(res);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I work with JavaScript on my bachelor thesis.
I would like to read a string and mark the elements that are the same and follow each other in the same color.
I have not found a good approach yet.
I would be grateful for a good proposal.
This is what i have right now.
function colorsetting(input){
var collength = input.length;
var now = '', last2 = '', colored = '';
now = last2 = input[0];
for(var i = 1; i <= collength, i++){
if(now !== last2){
colored = last2.fontcolor("green");
last2 = now;
}
now = input[i];
}
colored = last2.fontcolor("red");
return colored;
}
You can split up your input string using a regex:
/(.)\1*/g
(.) grabs any character, and stores that in capture group 1.
\1* then tells the regex to match as many of those as it can.
Then, iterate over that array, wrap the strings in a span, each with their own colour.
const str = "aaaabbbb123aaaaccccz";
const result = str.match(/(.)\1*/g);
console.log(result);
result.forEach(t => {
// Create a span element
const span = document.createElement("span");
// Set the text in that span to be the current match
span.innerText = t;
// Set the span's color to something random.
span.style.color = "#"+((1<<24)*Math.random()|0).toString(16);
document.body.append(span);
})
(random color code from here)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need your help on my new project
I hate regular expressions and its rules but i must use it this project.
want do this replace
var aString = '[bkz:sample key]';
I want get into key variable 'sample key' value from this aString
var key,clean;
key = 'sample key';
clean = cleanChars(key);
// clean = sample_key
//my target
key
how can i do this?
thanks in advance
function extractKey(str) {
var match = (str || '').match(/^\[bkz:(.+)\]$/);
return match? match[1] : '';
}
extractKey('[bkz:sample key]'); //sample key
var aString = "[bkz:sample key]";
var regex = /^\[(.+)\:(.+)\]$/;
var matches = regex.exec(aString);
// "sample key" should now be in matches[2]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array likes this
<script>
var myFruit = new Array("Apple", "orange", "banana", "plum", "pear");
var myindex = prompt("enter 0 to 4","");
document.write(myFruit[myindex]);
</script>
It will prompt a window and user enter a number then get result.
Now I don't want a prompt, I want to display the fruit in order like: My 1 fruit is an Aplle.
How can I write a code so that when I enter 1, then it will show: My 1 fruit is an (image) Apple. When I enter 2, it will show: My 2 fruit is an (image) orange....
I don't know how to get the index changed, then it will show both index and value simultaneously.
Please give me a hand . Thanks
------------------------------------------------------
From the help of user2782160, I have another concern below
I have 5 images put in an array likes this
<script>
var myDegree = new Array("30.gif", "60.gif", "90.gif", "120.gif", "150.gif");
.........
</script>
I need to write code somehow to have
<div id="degreeCount"> 30</div><div>//image 30.gif has to show inside this div</div>
I mean 30.gif will show next to the number 30.
Please give me a hand. Thank you!
Just concatenate the strings:
var myindex = parseInt(prompt("enter 0 to 4", ""), 10);
document.write ('My ' + myindex + ' fruit is an ' + myFruit[myindex]);
something like this i guess?
for(i = 0; i<myFruit.length; i++){
document.write("My "+i+" fruit is an <img src='URL HERE MINUS FRUIT NAME'"+myFruit[i]+".jpg>");
}