javascript - Split two sentences into words and characters and show them individually -
please me split 2 sentences array of words , characters , show them individually.
my input string this:
if didn't cry, give sweets. give home-made cakes.
what had done till given below:
$(function() { generatewords(); function generatewords() { $("#splitedwords").html(""); var input = "if didn't cry, give sweets. give home-made cakes."; var outputarray = input.split(/\s+/); // spliting each word // printing sorted words inside button (var = 0; < outputarray.length; i++) { $("#splitedwords").append($('<span>' + outputarray[i] + '</span><br/>')); } } });
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> </head> <body> <h4>input</h4> <p>if didn't cry, give sweets. give home-made cakes.</p> <h4>o/p required <small>( want split words, dot, comma, hyphen, semicolon etc array)</small></h4> <p> var outputarray = [ "if", "you", "didn't", "cry", ",", "i", "will", "give", "you", "sweets", ".", "then", "i", "will", "give", "you", "home-made", "cakes", "." ]; </p> <h4>actual o/p</h4> <p id="splitedwords"></p> </body> </html>
is in right way? appreciated :)
what you're trying splitting text tokens. if search, you'll find limitless solutions problem, language parsers in order further analyze text. , suggest doing so, that's useful stuff familiar with.
you can lot of methods, regex being one, not easiest of them.
[!#%&\'*+,\-./:;<=>?@\\\^_\`|~](?!\w)|[\"\)\(\]\[{}]|[^\s]*?(?=[!\"\#%&\'()*+,\-./:;<=>?@[\\\]^_\`{|}~]?(?:\s|$))
is came on spot all matches being of tokens, seen here, put them array.
array = yourstring.match(/[!#%&\'*+,\-./:;<=>?@\\\^_\`|~](?!\w)|[\"\)\(\]\[{}]|[^\s]*?(?=[!\"\#%&\'()*+,\-./:;<=>?@[\\\]^_\`{|}~]?(?:\s|$))/)
should provide results you're looking for.
Comments
Post a Comment