Javascript REGEX只在字符串的开头和结尾删除字符,而不是在中间

时间:2017-10-09 17:02:00

标签: javascript regex

我需要删除某些字符串中的“+”字符。

我正在使用这个正则表达式:

error: g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/aList.o.d" -o build/Debug/Cygwin-Windows/aList.o aList.cpp
In file included from aList.cpp:2:0:
aList.h:22:68: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const aList<T>&)’ declares a non-template function [-Wnon-template-friend]
             friend ostream& operator<< (ostream& os,const aList<T>&);
                                                                    ^
aList.h:22:68: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
mkdir -p dist/Debug/Cygwin-Windows
g++     -o dist/Debug/Cygwin-Windows/alist build/Debug/Cygwin-Windows/_ext/2ebfcef6/newstring.o build/Debug/Cygwin-Windows/aList.o build/Debug/Cygwin-Windows/alist1.o 
build/Debug/Cygwin-Windows/alist1.o: In function `main':
/cygdrive/c/Users/Zunera Jamal/Documents/NetBeansProjects/aList/alist1.cpp:50: undefined reference to `operator<<(std::ostream&, aList<newstring> const&)'
/cygdrive/c/Users/Zunera Jamal/Documents/NetBeansProjects/aList/alist1.cpp:50:(.text+0x2ca): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `operator<<(std::ostream&, aList<newstring> const&)'

问题是,当我有像“+++++ test + string +++”这样的字符串时,它显然会转换为 teststring ,正确的结果应该是 test + string

所以我需要的是找出一个正则表达式,删除“+”字符,只在字符串的开头和结尾。

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用

^\++|\++$

a demo on regex101.com

<小时/> 在JavaScript

string.replace(/^\++|\++$/g, "");