/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] cut a continues string at 32nd position

I m a js beginer.. i was trying to write a function to cut my string at every 32nd position if there was no spaces.

Explanation:

If the string have spaces dont cut. else if the string is long (no space) then at 32 nd point i want to include a space. again i want to continue and want to include a space at next 33rd postion if there was no spaces till that.

Please anyone help me to write this code..

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@007JulienMar 10.2012 — Try this code
[CODE]<body>
<label>Before<br><textarea id="bfr" rows="10" cols="32"></textarea></label><br>
<label>After<br><textarea id="ftr" rows="10" cols="32"></textarea></label>

<script type="text/javascript">
var str="Loremipsumdolor sitametconsectetueregetegetetutPhasellus. PharetraQuisquetemporsemconsectetuerSedsapienelitutVivamus Vestibulum. VitaeconvallisfeugiatantecursusProincondimentumauctormetuselitNunc. PellentesqueNamVivamus.";
document.getElementById('bfr').innerHTML=str;
[COLOR="Blue"]var newStr = str.replace(/([w]{31})(?=w)/ig,'$1 ');[/COLOR]
document.getElementById('ftr').innerHTML=newStr;
</script>
</body>[/CODE]
We replace any suit of 31 characters followed by another character by this suit (the first sub-pattern $1) followed by a space. With the ?= (a lookahead positive assertion) the second sub-pattern is not matched and count for the possible following pattern (Lookbehind assertions are not supported with javascript).

Edit : With the punctuation, it would be better to work with non space characters
[CODE][COLOR="Blue"]var newStr = str.replace(/([^s]{31})(?=[^s])/ig,'$1 ');[/COLOR][/CODE]
Copy linkTweet thisAlerts:
@johnrockauthorMar 13.2012 — Try this code
[CODE]<body>
<label>Before<br><textarea id="bfr" rows="10" cols="32"></textarea></label><br>
<label>After<br><textarea id="ftr" rows="10" cols="32"></textarea></label>

<script type="text/javascript">
var str="Loremipsumdolor sitametconsectetueregetegetetutPhasellus. PharetraQuisquetemporsemconsectetuerSedsapienelitutVivamus Vestibulum. VitaeconvallisfeugiatantecursusProincondimentumauctormetuselitNunc. PellentesqueNamVivamus.";
document.getElementById('bfr').innerHTML=str;
[COLOR="Blue"]var newStr = str.replace(/([w]{31})(?=w)/ig,'$1 ');[/COLOR]
document.getElementById('ftr').innerHTML=newStr;
</script>
</body>[/CODE]
We replace any suit of 31 characters followed by another character by this suit (the first sub-pattern $1) followed by a space. With the ?= (a lookahead positive assertion) the second sub-pattern is not matched and count for the possible following pattern (Lookbehind assertions are not supported with javascript).

Edit : With the punctuation, it would be better to work with non space characters
[CODE][COLOR="Blue"]var newStr = str.replace(/([^s]{31})(?=[^s])/ig,'$1 ');[/COLOR][/CODE][/QUOTE]


Yeah.. this is working fine... But this code seems difficult to understand for me. I dint see this type of code in js before. Can you tell me where i can learn these basics.. esp this part " (/([^s]{31})(?=[^s])/ig,'$1 '); "
Copy linkTweet thisAlerts:
@TheAliveWinnerMar 13.2012 — Learn [B]RegExp[/B]: JS RegExp
Copy linkTweet thisAlerts:
@johnrockauthorMar 13.2012 — Learn [B]RegExp[/B]: JS RegExp[/QUOTE]

thanks bro..!!
Copy linkTweet thisAlerts:
@007JulienMar 13.2012 — You find probably that i, for ignore case, is not useful with non space characters, which could be match with S. Then a shorter regular expression is :

[CODE][COLOR="Blue"]var newStr = str.replace(/(S{31})(?=S)/g,'$1 ');[/COLOR][/CODE]
×

Success!

Help @johnrock spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...