简体   繁体   English

如何使用JavaScript正则表达式删除所有'#'符号(如果它们在单词开头)

[英]How to remove all '#' symbols if they are at the start of word using javascript regular expression

I have a string and need to remove all '#' symbols, but only in case when they are placed at the beginning of the word. 我有一个字符串,需要删除所有的“#”符号,但前提是将它们放在单词的开头。 For example having next string: 例如,具有下一个字符串:

"#quick ##brown f#x"

Should be transformed into: 应转换为:

"quick brown f#x"

How it could be achieved using regular expressions in javascript? 使用javascript中的正则表达式如何实现? Thank you in advance for help 预先感谢您的帮助

var a = "#quick ##brown f#x"

var e = /(^| )#+/g
a.replace(e, "$1")

That should do the trick. 这应该够了吧。

use like this (\\s+)\\#+|^# .It will be prevent middle of the # 这样使用(\\s+)\\#+|^#它将防止#中间出现

Demo 演示版

 console.log("#quick ##brown f#x".replace(/(\\s+)\\#+|^#/g,"$1")) 

You could either split the string and replace every hash # sign that is at the beginning of the string. 你既可以分割字符串和替换每一个散列#标志是在字符串的开头。

 var str = "#quick ##brown f#x", res = str.split(' ').map(v => v.replace(/^#+/, '')).join(' '); console.log(res); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式或纯 JavaScript 从字符串中删除所有括号及其内容并保留其他文本格式 - How to remove all parenthesis and their contents from a string using a regular expression or pure JavaScript and retain other text format PHP 正则表达式删除所有 javascript 异常 - PHP regular expression to remove all javascript with exception 在javascript中使用正则表达式提取单词 - extract word using regular expression in javascript 是否存在类似于Vim正则表达式的单词原子“\\ &lt;”和“\\&gt;”的开头和结尾的JavaScript等价物? - Are there JavaScript equivalents of the Vim regular expression start and end of word atoms “\<” and “\>”? 正则表达式可通过特定符号查找单词 - Regular Expression to find word by specific symbols 在 javascript 中使用正则表达式删除 \n - Remove \n using regular expression in javascript 如何使用一些特殊符号为数字创建 JavaScript 正则表达式 - How to create the JavaScript regular expression for number with some special symbols 使用正则表达式在单词开头和冒号之间转义特殊字符 - Escape special characters between the start of a word and a colon using Regular Expression 如何使用正则表达式选择单词的一部分? - How to select part of the word using regular expression? 使用正则表达式删除每个单词末尾的特定字符 - Remove specific character at the end of each word using regular expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM