i creating simple program prompts user type in filename extension , program separate filename , rootname. essentially, goal looks below:
userinput=jack.html filename=jack rootname=html
i able separate using split function , accessing separately problem when user input has multiple periods example this
userinput= jack.1.2.html filename=jack.1.2 rootname=html
how go separating user input when has multiple periods. here code. want work multiple periods well:
var userinput, splitinput; var rootname, filename; userinput= prompt('enter filename:') // splitinput=userinput.split('.') filename= splitinput[0] rootname=splitinput[1]+ userinput[2] console.log(filename) console.log(rootname)
you can accomplish using pop()
, join()
. split string periods, pop off last one, , 2 sections of string (rootname , filename).
here live demo:
var userinput = prompt('enter filename:'); var splitinput = userinput.split('.'); var rootname = splitinput.pop(); var filename = splitinput.join("."); document.getelementbyid("output1").textcontent = filename; document.getelementbyid("output2").textcontent = rootname;
<div>filename: <span id="output1"></span></div> <div>rootname: <span id="output2"></span></div>
Comments
Post a Comment