简体   繁体   English

正则表达式在开始时将任何字符与特定字符串匹配

[英]Regular expression match any character at the beginning end with specific string

I have the following string: 我有以下字符串:

"bar foo test-line drop-line"

I need to replace the words that starts with anything and ends with '-line'. 我需要替换以任何东西开头并以“ -line”结尾的单词。

basically having: 基本具有:

"bar foo new_word new_word"

I tried: 我试过了:

string.replace(/\.-line$/g,'new_word')

but it doesn't work. 但这不起作用。

Try this as a regex: 尝试将此作为正则表达式:

/\S+-line(?![-\w])/

The word anchor is not suitable here since dashes are not considered part of a word, so /\\S+-line\\b/ would mistakenly match text-with-line-not-to-be-replaced . 锚点一词在这里不合适,因为破折号不被认为是单词的一部分,因此/\\S+-line\\b/会错误地匹配text-with-line-not-to-be-replaced Hence the lookahead construct. 因此,先行构造。

Of course, according to your use case, \\S may seem a little coarse. 当然,根据您的用例, \\S可能看起来有些粗糙。 If your words really only consist of letters then dashes etc, then you can use the normal* (special normal*)* pattern: 如果您的单词实际上只包含字母,然后是破折号等,那么您可以使用normal* (special normal*)*模式:

/[a-z]+(-[a-z]+)*-line(?![-\w])/i

(normal: [az] , special: - ) (正常: [az]特殊: -

(edit: changed the lookahead construct, thanks to @thg435) (编辑:感谢@ thg435更改了lookahead构造)

In your regular expression, you can use \\b to detect beginning of new words and \\w for "word characters": 在正则表达式中,您可以使用\\ b来检测新单词的开头,并使用\\ w来表示“单词字符”:

"your string".replace(/\b\w+-line\b/g,'new_word')

See http://www.w3schools.com/jsref/jsref_obj_regexp.asp 参见http://www.w3schools.com/jsref/jsref_obj_regexp.asp

How about 怎么样

string.replace(/\b\S+-line\b/g,'new_word')

\\b Word boundary \\b字边界

\\S non whitespace \\S非空格

As fge astutely notes, we need a \\b after line so we don't catch things ending lines liner etc etc etc 作为FGE敏锐地指出,我们需要一个\\bline ,所以我们不抓住事物结束lines liner等等等等

暂无
暂无

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

相关问题 正则表达式是否可以匹配字符串开头或结尾的字符(但不能同时匹配)? - Can a regular expression match a character at the beginning OR end of the string (but not both)? 如何生成不包含任何空格的正则表达式,甚至在字符串的开头或结尾也不包含任何空格 - How to generate Regular Expression that doesn't contain any white space at all and even at the beginning or the end of the string 正则表达式 - 匹配任何不以+开头但允许+1的字符串 - Regular Expression - Match any string not starting with + but allow +1 如何用正则表达式匹配特定的字符串? - How to match a specific string with regular expression? 正则表达式 - 匹配除+之外的任何字符,空字符串也应匹配 - Regular Expression - Match any character except +, empty string should also be matched Javascript使用正则表达式匹配以字符集中的特定字符开头并以相同字符结尾的字符串 - Javascript match a string which start with a specific char from the set of chars and end with same char using Regular Expression 正则表达式-在不使用$符号的情况下匹配字符 - Regular expression - match character(s) at the end without using $ sign 替换以特定字符开头并以正则表达式中的特定字符结尾的字符串 - Replace string that starts with specific character and ends with specific character in regular expression 如何替换字符串开头和结尾的特定字符? - How can I replace the specific character beginning and end of string? JavaScript 正则表达式匹配结尾带有任何标点符号的单词 - JavaScript regular expression to match a word with any kind of punctuation at the end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM