在函数之前用变量Javascript中的空格替换新行

时间:2016-08-19 08:30:57

标签: javascript function replace line-breaks

我有以下代码snipplet:

<script>
    var textstring = 
    'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. 
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';

function insertNewLines(textstr, nos) {

var resulttext = '';
textstr = textstr.replace("\n", "<br />");
while (textstr.length > 0) {

resulttext += textstr.substring(0, nos) + '<br/>';
textstr = textstr.substring(nos);

}

textstring中的文本是2行,即有一个\ r \ n分隔第二行的第一行。 当我调用该函数时,它甚至在我得到替换部件之前就给出了一个错误。 (调试器说因为断行而没有​​正确关闭块)

有没有办法解决这个问题?我只需要摆脱换行,甚至更好地保留它,但让脚本正确运行。

感谢您的帮助。

TheVagabond

4 个答案:

答案 0 :(得分:2)

该代码中存在两个基本语法错误:

  1. 除非用反斜杠转义它,否则不能在字符串文字中添加换行符。 (您可以在ES2015模板文字中,但不能使用字符串文字。)如果这样做,则换行符不包含在字符串中。请改用 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="packagesToScan"> <array> <value>my.entity.store.package</value> </array> </property> <property name="persistenceUnitName" value="mainPersistenceUnit" /> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <property name="jpaDialect" ref="jpaDialect" /> <property name="jpaPropertyMap"> <map> <entry key="hibernate.hbm2ddl.auto" value="validate" /> <entry key="hibernate.show_sql" value="true" /> <entry key="hibernate.format_sql" value="true" /> <entry key="hibernate.proc.param_null_passing" value="true" /> </map> </property> </bean>

  2. 您错过了函数

  3. 上的结束\r\n

    除此之外,不需要循环; JavaScript的}可以接受带有replace标记的正则表达式(对于“global”),所以:

    g

    我已使用var textstring = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.\r\nCum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.'; function insertNewLines(textstr, nos) { return textstr.replace(/\r\n/g, "<br />"); } console.log(insertNewLines(textstring));替换了文字换行符,然后使用\r\n的单个调用替换了循环。

答案 1 :(得分:-1)

首先确定你的刺痛lilke,

var textstring ='Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.\
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';

在你的函数中添加一个括号,它应该可以工作。

function insertNewLines(textstr, nos) {

var resulttext = '';
textstr = textstr.replace("\n", "<br />");
while (textstr.length > 0) {

resulttext += textstr.substring(0, nos) + '<br/>';
textstr = textstr.substring(nos);

}
}

答案 2 :(得分:-1)

JavaScript字符串中的换行符应使用反斜杠(public class PlayPauseDrawable extends Drawable { private static final Property<PlayPauseDrawable, Float> PROGRESS = new Property<PlayPauseDrawable, Float>(Float.class, "progress") { @Override public Float get(PlayPauseDrawable d) { return d.getProgress(); } @Override public void set(PlayPauseDrawable d, Float value) { d.setProgress(value); } }; private final Path mLeftPauseBar = new Path(); private final Path mRightPauseBar = new Path(); private final Paint mPaint = new Paint(); private final RectF mBounds = new RectF(0,0,10,10); private final float mPauseBarWidth; private final float mPauseBarHeight; private final float mPauseBarDistance; private float mWidth; private float mHeight; private float mProgress; private boolean mIsPlay; public PlayPauseDrawable(Context context) { final Resources res = context.getResources(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.DKGRAY); mPauseBarWidth = res.getDimensionPixelSize(R.dimen.pause_bar_width); mPauseBarHeight = res.getDimensionPixelSize(R.dimen.pause_bar_height); mPauseBarDistance = res.getDimensionPixelSize(R.dimen.pause_bar_distance); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mBounds.set(bounds); mWidth = mBounds.width(); mHeight = mBounds.height(); } @Override public void draw(Canvas canvas) { mLeftPauseBar.rewind(); mRightPauseBar.rewind(); // The current distance between the two pause bars. final float barDist = lerp(mPauseBarDistance, 0, mProgress); // The current width of each pause bar. final float barWidth = lerp(mPauseBarWidth, mPauseBarHeight / 2f, mProgress); // The current position of the left pause bar's top left coordinate. final float firstBarTopLeft = lerp(0, barWidth, mProgress); // The current position of the right pause bar's top right coordinate. final float secondBarTopRight = lerp(2 * barWidth + barDist, barWidth + barDist, mProgress); // Draw the left pause bar. The left pause bar transforms into the // top half of the play button triangle by animating the position of the // rectangle's top left coordinate and expanding its bottom width. mLeftPauseBar.moveTo(0, 0); mLeftPauseBar.lineTo(firstBarTopLeft, -mPauseBarHeight); mLeftPauseBar.lineTo(barWidth, -mPauseBarHeight); mLeftPauseBar.lineTo(barWidth, 0); mLeftPauseBar.close(); // Draw the right pause bar. The right pause bar transforms into the // bottom half of the play button triangle by animating the position of the // rectangle's top right coordinate and expanding its bottom width. mRightPauseBar.moveTo(barWidth + barDist, 0); mRightPauseBar.lineTo(barWidth + barDist, -mPauseBarHeight); mRightPauseBar.lineTo(secondBarTopRight, -mPauseBarHeight); mRightPauseBar.lineTo(2 * barWidth + barDist, 0); mRightPauseBar.close(); canvas.save(); // Translate the play button a tiny bit to the right so it looks more centered. canvas.translate(lerp(0, mPauseBarHeight / 8f, mProgress), 0); // (1) Pause --> Play: rotate 0 to 90 degrees clockwise. // (2) Play --> Pause: rotate 90 to 180 degrees clockwise. final float rotationProgress = mIsPlay ? 1 - mProgress : mProgress; final float startingRotation = mIsPlay ? 90 : 0; canvas.rotate(lerp(startingRotation, startingRotation + 90, rotationProgress), mWidth / 2f, mHeight / 2f); // Position the pause/play button in the center of the drawable's bounds. canvas.translate(mWidth / 2f - ((2 * barWidth + barDist) / 2f), mHeight / 2f + (mPauseBarHeight / 2f)); // Draw the two bars that form the animated pause/play button. canvas.drawPath(mLeftPauseBar, mPaint); canvas.drawPath(mRightPauseBar, mPaint); canvas.restore(); } public Animator getPausePlayAnimator() { final Animator anim = ObjectAnimator.ofFloat(this, PROGRESS, mIsPlay ? 1 : 0, mIsPlay ? 0 : 1); anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mIsPlay = !mIsPlay; } }); return anim; } public boolean isPlay() { return mIsPlay; } private void setProgress(float progress) { mProgress = progress; invalidateSelf(); } private float getProgress() { return mProgress; } @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); invalidateSelf(); } @Override public void setColorFilter(ColorFilter cf) { mPaint.setColorFilter(cf); invalidateSelf(); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } /** * Linear interpolate between a and b with parameter t. */ private static float lerp(float a, float b, float t) { return a + (b - a) * t; } )进行转义。

您的变量\也需要更新,如下所示。

textstring

答案 3 :(得分:-1)

您必须使用反斜杠转义文字字符串,也没有右括号。下面的代码使用反斜杠进行转义,脚本正在运行。但是输出是您想要的未经检查的。

var textstring = 
'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo  ligula eget dolor. Aenean massa.\
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';

insertNewLines(textstring,2);
function insertNewLines(textstr, nos) {

var resulttext = '';
textstr = textstr.replace("\n", "<br />");
while (textstr.length > 0) {

resulttext += textstr.substring(0, nos) + '<br/>';
textstr = textstr.substring(nos);

 }
}