javascript - How to return part of string after a certain character? -


if @ jsfiddle question,

var str = "abc: lorem ipsum sit amet"; str = str.substring(str.indexof(":") + 1); 

this returns characters after :, how can adjust return characters before :

something var str_sub = str.substr(str.lastindexof(":")+1); not work.

you fiddle job ... maybe try string before double colon? (you should edit question) code go this:

str.substring(0, str.indexof(":"));

where 'str' represents variable string inside.

click here jsfiddle example

javascript

var input_string = document.getelementbyid('my-input').innertext; var output_element = document.getelementbyid('my-output');  //here go var right_text = input_string.substring(0, input_string.indexof(":"));  output_element.innertext = right_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; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -