Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在“处换行”-&引用;_Javascript_Reactjs - Fatal编程技术网

Javascript 在“处换行”-&引用;

Javascript 在“处换行”-&引用;,javascript,reactjs,Javascript,Reactjs,如果字符串包含“-”,我将尝试创建单独的行。 但如果它不包含“-”,则保留字符串的原样。 那么比如说, const string = '-Sentence one -Sentence two -Sentence three' <div> {string} </div> const string='-句子一-句子二-句子三' {string} 但是,如果后面有一个空格和一个连字符,那么这个字符串应该被分成多行。 那么应该在网站上显示的是几行。而不是一句话。 另外,在

如果字符串包含“-”,我将尝试创建单独的行
。
但如果它不包含“-”,则保留字符串的原样。
那么比如说,

const string = '-Sentence one -Sentence two -Sentence three'
<div>
  {string}
</div>
const string='-句子一-句子二-句子三'
{string}
但是,如果后面有一个空格和一个连字符,那么这个字符串应该被分成多行。 那么应该在网站上显示的是几行。而不是一句话。
另外,在替换之后,我还希望重新插入“-”字符。

您可以在字符串上使用.replaceAll,将
“-”
替换为
“\n”


您需要将div的
空白
css属性设置为可以使用的
前置行


您需要将div的
空白
css属性设置为
前置行

,您可以使用split和join来完成此操作

这里有一条班轮:

const string = '-Sentence one -Sentence two -Sentence three';
string.split(' -').join('<br>') // if line break in html
string.split(' -').join('\n') // or this
const string='-句子一-句子二-句子三';
string.split('-').join('
')//如果html中有换行符 string.split('-').join('\n')//或

或者,您可以在拆分后在数组上运行循环,并通过使用新行添加所需元素来显示它们。

您可以使用拆分和联接来完成此操作

这里有一条班轮:

const string = '-Sentence one -Sentence two -Sentence three';
string.split(' -').join('<br>') // if line break in html
string.split(' -').join('\n') // or this
const string='-句子一-句子二-句子三';
string.split('-').join('
')//如果html中有换行符 string.split('-').join('\n')//或

或者,您可以在拆分后在数组上运行循环,并通过使用新行添加所需元素来显示它们。

考虑拆分、映射并添加换行符(

有点像:

const string = '-Sentence one -Sentence two -Sentence three'

<div>
  {string.split(' -').map(x => <span key={somethingUnique}>-{x} <br /></span>)}
</div>
const string='-句子一-句子二-句子三'
{string.split('-').map(x=>-{x}

考虑拆分、映射和添加换行符(

有点像:

const string = '-Sentence one -Sentence two -Sentence three'

<div>
  {string.split(' -').map(x => <span key={somethingUnique}>-{x} <br /></span>)}
</div>
const string='-句子一-句子二-句子三'
{string.split('-').map(x=>-{x}

尝试使用
split()
?但是我不需要拆分包含“-”的单词,因此如果我使用“-”,它似乎不起作用。请用更多的例子来澄清这个问题?另外,是否要在HTML中呈现带有换行符的句子@Joshuabitton如果你有
“-这不酷”
?是的,在html中,这也是为什么我想在空格后拆分“-”并尝试使用
split()
?但是我不需要拆分包含“-”的单词,所以如果我有“-”,它似乎不起作用。请用更多的例子来澄清这个问题?另外,是否要在HTML中呈现带有换行符的句子@Joshuabitton如果你有
“-这不是很酷”
?是的,在html中,这也是为什么我想在空格后拆分“-”你也可以在拆分后重新插入破折号吗?我没有告诉你拆分,我告诉过你使用替换,但是是的,你可以用“\n-”替换“-”,在破折号之前添加换行符拆分破折号后,您还可以重新插入破折号吗?我没有告诉您要拆分,我告诉过您使用“替换”,但可以使用“\n-”替换“-”,而是在破折号之前添加换行符您是对的@NguyễnVănPhong,但我使用映射来更好地控制添加换行符后每个字符串的外观。当您只进行拆分和联接时,它会将“-”替换为
@NguyễnVănPhong事实上根本不一样,因为这不是HTML,而是React的JSX。你是对的@NguyễnVănPhong,但我使用映射来更好地控制添加换行符后每个字符串的外观。当您只进行拆分和联接时,它会将“-”替换为
@NguyễnVănPhong事实上完全不同,因为这不是HTML,而是React的JSX。