Reputation: 398
I have a text area which is filled from database. In that textarea comma's are replaced by white space using this.
document.getElementById('<%=fnl000Db000MESSAGE.ClientId %>').value=document.getElementById('<%=fnl000Db000MESSAGE.ClientId %>').value.replace(/,/g,'\s');
Now I want to insert a new a new line after first two words like this.. eg.. hello admin '\n' welcome
how can i get this using javascript. Do i need to use Regex or any other way.
I want an output like
Hello Admin
Process are running...
Upvotes: 1
Views: 886
Reputation: 16828
How about
"hello admin welcome".replace(/^(\w+\s+\w+)/, '$1\n');
EDIT:
The regex satisfies your requirement, which is to match the first two words, regardless of the number of spaces or the length of the words. Another approach, which is a bit more flexible in that you can specify the number of words to match would be
"hello admin welcome".replace(/^(([\w]+\s+){2})/, '$1\n');
Upvotes: 3
Reputation: 39872
This isn't all fancy like a regex, but I find it easier to read =).
var foo = 'Hello Admin Process are running....';
var secondSpaceIndex = foo.indexOf(' ', foo.indexOf(' ') + 1);
var foo2 = foo.substr(0,secondSpaceIndex) + '\n' + foo.substr(secondSpaceIndex + 1);
Upvotes: 1